杭电acm1004

来源:互联网 发布:关于淘宝规则的变更 编辑:程序博客网 时间:2024/04/28 15:40

自己写了代码,但是总是时间受限,所以在网上寻求帮助,下面是从网上看到了不错的代码:

#include <map>

#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
string str;
map<string,int> strMap;
while(cin>>n && n)
{
strMap.clear();
for(int i = 0; i < n; ++ i)
{
cin>>str;
int t = strMap[str];
if(t)
{
strMap[str] ++;
}
else
{
strMap[str] = 1;
}
}
int nTemp;
string strTemp;
map<string,int>::const_iterator it = strMap.begin();
nTemp = it->second;
strTemp=it->first;
while(it != strMap.end())
{
if(it->second > nTemp)
{
nTemp = it->second;
strTemp = it ->first;
}
it ++;
}
cout<<strTemp<<endl;
}
return 0;
}
0 0
原创粉丝点击