题目链接:传送门
思路:直接遍历字符串,筛选出单词,然后计数即可。
代码:
#include <bits/stdc++.h>
using namespace std;
map <string , int> mp;
int main() {
string s;
ios::sync_with_stdio(0);
getline(cin , s);
string tmp = "";
string t = "";
int ans = 0;
for(int i = 0 ; i < s.length() ; i++) {
if(!islower(s[i]) && !isupper(s[i]) && !isdigit(s[i])) {
if(tmp != "") {
mp[tmp]++;
int num = mp[tmp];
if(ans < num || (t > tmp && ans == num)) {
ans = num;
t = tmp;
}
}
tmp = "";
}
else {
tmp += tolower(s[i]);
}
}
if(tmp != "") {
mp[tmp]++;
int num = mp[tmp];
if(ans < num || (t > tmp && ans == num)) {
ans = num;
t = tmp;
}
}
cout << t << " " << ans << "\n";
return 0;
}