字符串处理-Hdu1004

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004

题目大意:给你一个数n,要求输入n个字符串,在这n个字符串中,我们需要输出出现次数最多的字符串。

这道题看起来的确很简单,但是却花了我不少时间,开始时是利用C语言中的char数组来存这些字符串,依次统计,但是做了很久都始终有错,于是我便转变思路,利用C++中的string来村字符串。

注意:这里有一个小小的知识点,用string来定义的字符串,最好使用cout来输出,如果用printf函数来输出的话会出现乱码。(具体的可以参考博客:https://blog.csdn.net/cjolj/article/details/55267660)

代码

#include<cstring>
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int n;
    while(cin>>n&&n){
        int v;
        string b[n];
        v=n;
        while(n--){
            cin>>b[n];
        }
        int r[v];//用来存放每个串出现的次数
        for(int i=0;i<v;i++){
            r[i]=0;
            for(int j=0;j<v;j++){
                if(b[i]==b[j]) r[i]++;
            }
        }
        int max1=0;
        for(int i=0;i<v;i++){
            if(r[max1]<=r[i]){
                max1=i;
            }
        }
        cout<<b[max1]<<endl;
    }
    return 0;
}

 

posted @ 2018-11-03 19:43  里昂静  阅读(797)  评论(0编辑  收藏  举报