HDU1004

#include <bits/stdc++.h>
using namespace std;
int main()
{
    map<string,int>::iterator it;
    map<string,int> p;
    string s,ans;
    int n;
    while(cin>>n,n)
    {
        p.clear();//否则上一次的仍会被记录 
        for(int i=1;i<=n;i++)
        {
            cin>>s;
            p[s]++;
        }
        int maxn=0;
        for(it=p.begin();it!=p.end();it++)
        {
            if(it->second>maxn)
            {
                maxn=it->second;
                ans=it->first;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

受UVa540的影响,刚刚学了map,UVa540中用map<int,int>写了一个数组,于是就想着用map<string,int>写一个记录string类型的数组,第一次没有清空map,长了教训

原文地址:https://www.cnblogs.com/benzikun/p/10505629.html