跳到正文

题解

题解:P15154 [SWERC 2024] The king of SWERC

原文
文章信息与版本

文章信息

收录
2026年2月4日
更新
2026年2月4日
篇幅
约 1 分钟
题目
P15154

版本

  1. v1当前

抢到洛谷首 A。

只需要用 map 统计每个名字的出现次数,然后找出出现次数最多的那个即可。

由于时间限制过于短,所以使用复杂度更低的 unordered_map。

最后用 pair 遍历即可。

AC code

AC code
C++
#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;
}
原文