有多组测试数据,需要看清楚
程序设计竞赛时,赛场升起各色气球多么激动人心呀!志愿者送气球忙得不亦乐乎,观战的某人想知道目前哪种颜色的气球送出最多。
输入格式:
测试数据有多组,处理到文件尾。每组数据先输入一个整数n(0<n≤5000)表示分发的气球总数。接下来输入n行,每行一个表示颜色的字符串(长度不超过20且仅由小写字母构成)。
输出格式:
对于每组测试,输出出现次数最多的颜色。若出现并列的情况,则只需输出ASCII码值最小的那种颜色。
输入样例:
3
pink
red
pink
输出样例:
pink
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int n;
vector< pair<string,int> > color;
bool cmp(pair<string,int> x,pair<string,int> y) {
if(x.second==y.second) return x.first<y.first;
return x.second>y.second;
}
int main() {
while(cin>>n) {
while(n--) {
string s;
cin>>s;
bool flag=false;
if(!color.empty()) {
for(int i=0; i<color.size(); i++) {
if(color[i].first==s) {
flag=true;
color[i].second++;
break;
}
}
if(!flag) color.push_back((pair<string,int>) {s,1});
} else {
color.push_back((pair<string,int>) {s,1});
}
}
sort(color.begin(),color.end(),cmp);
cout<<color[0].first<<endl;
color.clear();
}
return 0;
}