很简单的题,看数据量排序可以轻松搞定,不需要hash。
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string maxcolor(string a[],int length)
{
if(length==0)
return "";
int current=1,maxn=1;
sort(a,a+length);
string maxcolor=a[0];
for(int i=2;i<length;i++)
{
if(a[i]!=a[i-1])
{
if(current>maxn)
{
maxcolor=a[i-1];
maxn=current;
}
current=1;
}
else
current++;
}
if(current>maxn)
maxcolor=a[length-1];
return maxcolor;
}
int main()
{
int n=0;
string colors[1000]={};
while(cin>>n)
{
for(int i=0;i<n;i++)
cin>>colors[i];
string color= maxcolor(colors,n);
if(color!="")
cout<<color<<endl;
}
}