Description
输入若干行字符串,输出出现次数最多的一条字符串。
Input
输入数据的第一行有一个整数N,表示有N行字符串(1 < N < 10^6)。
接下来有N行字符串,字符串只包含小写字母,长度不大于 100 , 不重复的字符串数目不超过 1000 。
Output
输出出现次数最多的一条字符串,数据保证结果唯一。
Sample Input
3
asdfghjkl
asdfghjkl
fefeff
Sample Output
asdfghjkl
题解
使用stl::map直接计数即可。
代码
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
class cmp{
public:
bool operator () (const pair<string,int> &a, const pair<string,int> &b){
return a.second<b.second;
}
};
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
string s;
map<string,int> m;
while(n--){
cin>>s;
m[s]++;
}
cout<<max_element(m.begin(),m.end(),cmp())->first<<endl;
return 0;
}