将判断字母数字和变为小写用相应的库函数
#include <iostream>
#include <unordered_map>
#include <cctype>
using namespace std;
int main()
{
string str;
getline(cin, str);
unordered_map<string, int> hash;
for (int i = 0; i < str.size(); i ++ )
if (isalnum(str[i]))
{
string word;
int j = i;
while (j < str.size() && isalnum(str[j])) word += tolower(str[j ++ ]);
hash[word] ++ ;
i = j;
}
string word;
int cnt = -1;
for (auto item : hash)
if (item.second > cnt || (item.second == cnt && item.first < word))
{
word = item.first;
cnt = item.second;
}
cout << word << ' ' << cnt << endl;
return 0;
}