杭電acm1004

自己寫了代碼,但是總是時間受限,所以在網上尋求幫助,下面是從網上看到了不錯的代碼:

#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;
}
發佈了34 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章