Description
输入若干行字符串,输出出现次数最多的一条字符串。
Input
输入数据的第一行有一个整数N,表示有N行字符串(1 < N < 10^6)。
接下来有N行字符串,字符串只包含小写字母,长度不大于 100 , 不重复的字符串数目不超过 1000 。
Output
输出出现次数最多的一条字符串,数据保证结果唯一。
Sample Input
3
asdfghjkl
asdfghjkl
fefeff
Sample Output
asdfghjkl
直接用c++STL里的map读取并统计字符串即可。
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(void)
{
int n;
cin >> n;
map<string, int> strs;
while (n--)
{
string temp;
cin >> temp;
strs[temp]++;
}
auto Max_iter = strs.begin();
for (auto iter = strs.begin(); iter != strs.end(); ++iter)
if (Max_iter->second < iter->second)
Max_iter = iter;
cout << Max_iter->first;
return 0;
}
目前古董校OJ并不支持c++11,相关代码还需修改一下。
如超时字符串读入用gets()。