【Hash】【Boyer-Moor】Colorful Balloons
今天和ssy吃饭,他和我说拉美赛区题目质量不一定好,所以开始做icpc2023 合肥题目
https://codeforces.com/gym/104857/problem/F
这是他当时写的签到题,用Boyer-Moor
投票法可以很轻松地搞定,但是他建议我用朴实的方法,因为空间不重要
学会了使用for(const auto& pair:mymap){pair.first, pair.second}
来遍历哈希的键和值,挺好用的
#include <iostream>
#include <map>
#include <string>
int main() {
int n;
std::cin >> n;
std::map<std::string, int> colorCount;
std::string color;
// Reading colors and counting occurrences
for (int i = 0; i < n; i++) {
std::cin >> color;
colorCount[color]++;
}
// Determine the color that appears more than n / 2 times
for (const auto& pair : colorCount) {
if (pair.second > n / 2) {
std::cout << pair.first << std::endl;
return 0; // Found the majority color, exit program
}
}
// If no majority color is found
std::cout << "uh-oh" << std::endl;
return 0;
}