抢到洛谷首 A。
只需要用 map 统计每个名字的出现次数,然后找出出现次数最多的那个即可。
由于时间限制过于短,所以使用复杂度更低的 unordered_map。
最后用 pair 遍历即可。
AC code
AC code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
unordered_map<string, int> count;
string name;
for (int i = 0; i < n; ++i) {
cin >> name;
count[name]++;
}
string king;
int mv = 0;
for (auto& pair : count) {
if (pair.second > mv) {
mv = pair.second;
king = pair.first;
}
}
cout << king << endl;
return 0;
}