有一堆单词,里面可能有大写字母或者小写字母。请你先将单词全部转为小写,找出小写单词里面出现最多的一个单词视为关键词,如果这样的单词有多个,那么找出其中字典序最小的一个。 两个字符串,大小关系取决于两个字符串从左到右第一个不同字符的 ASCII 值的大小关系。比如a小于b,ah1x小于ahb,acb小于b。 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M 输入描述: 输入的第一行输入这堆单词的个数,每行一个长度不超过 的字符串,代表一个单词。 一堆单词总共不会超过 个单词。
输出描述: 一行输出一个字符串以及一个正整数,代表提取出的关键词,以及关键词出现的次数。
示例1
输入例子: 10 Nice to meet you I can help you thank you
输出例子:
you 3
例子说明:
you最多出现了3次
示例2
输入例子: 3 b c ad
输出例子: ad 1
例子说明:
都出现了1次,ad的字典序最小
示例3
输入例子: 4 B b c ad
输出例子: b 2
C++语言代码如下:
#include <iostream>
#include <unordered_map>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
unordered_map<string, int> mp;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
transform(s.begin(), s.end(), s.begin(), ::tolower); // 转为小写
mp[s]++;
}
string res = "";
int max_cnt = 0;
for (auto& p : mp) {
if (p.second > max_cnt) {
res = p.first;
max_cnt = p.second;
} else if (p.second == max_cnt && p.first < res) {
res = p.first;
}
}
cout << res << " " << max_cnt << endl;
return 0;
}