原题链接:
https://pintia.cn/problem-sets/994805342720868352/exam/problems/type/7?problemSetProblemId=994805398257647616&page=0
代码:
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
#define endl '\n'
string str;
map<string, int> m;
bool isAlphanumerical(char c) {
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
return true;
}
else {
return false;
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
getline(cin,str);
string word = "";
vector<string> maxWords;
int maxCnt = 0;
for (int i = 0; i < str.size(); i++) {
if (isAlphanumerical(str[i])) {
if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = str[i] - 'A' + 'a';
}
word+=str[i];
}
else {
if (!word.empty()) {
m[word] += 1;
if (m[word] > maxCnt) {
maxWords.clear();
maxWords.push_back(word);
maxCnt = m[word];
}
else if (m[word] == maxCnt) {
maxWords.push_back(word);
}
word = "";
}
}
}
sort(maxWords.begin(), maxWords.end());
if(maxWords.size()!=0)
{
cout << maxWords[0] << " " << maxCnt;
}
else {
cout << word << " " << 1;
}
return 0;
}