首先看题目很容易就想到了用map因为map可以去掉重复的,但是引发一个问题怎么对value进行排序,于是百度了一下发现挺复杂的,然后想了很久没想出来怎么弄问了一下队友,用set来对map进行排序,很奇妙的方法!!!!,就是弄一个结构题把map的值放进去然后用set存储排序;
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <set>
using namespace std;
struct node{
string a;
int b;
bool operator <(const node & c)const
{
return c.b<b;
}
};
int main()
{
int n;
map<string,int> mp;
set <node> ans;
node a;
while(1)
{
mp.clear();
ans.clear();
scanf("%d",&n);
if(n==0)
break;
for(int i=0;i<n;i++)
{
string str;
cin>>str;
mp[str]++;
}
map <string,int>::iterator it;
for(it = mp.begin();it!=mp.end();it++)
{
node shuzu;
shuzu.a=it->first;
shuzu.b=it->second;
ans.insert(shuzu);
// cout<<shuzu.b<<endl;
}
set <node> ::iterator it1;
it1=ans.begin();
/*for(it1=ans.begin();it1!=ans.end();it1++)
{
cout<<(*it1).a<<" "<<(*it1).b<<endl;
}*/
cout<<(*it1).a<<endl;
}
return 0;
}