今天和ssy吃飯,他和我說拉美賽區題目質量不一定好,所以開始做icpc2023 合肥題目
https://codeforces.com/gym/104857/problem/F
這是他當時寫的簽到題,用Boyer-Moor投票法可以很輕鬆地搞定,但是他建議我用樸實的方法,因為空間不重要
學會了使用for(const auto& pair:mymap){pair.first, pair.second}來遍歷雜湊的鍵和值,挺好用的
`#include
include
include
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;
}
`