题目描述
C++代码
样例
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<unordered_map>
using namespace std;
int main()
{
string s;
unordered_map<string, int> mp;
getline(cin, s);
int sz = s.size();
string a;
for (int i = 0; i < sz; i++)
{
if (!a.size() && ((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')))
{
if (s[i] >= 'A' && s[i] <= 'Z') {
s[i] += 32;
}
a += s[i];
}
else {
if ((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) {
if (s[i] >= 'A' && s[i] <= 'Z') {
s[i] += 32;
}
a += s[i];
}
else if (a != "") {
mp[a]++;
a = "";
}
}
}
if (a.size())
{
mp[a]++;
}
int res = -1;
string p;
for (const auto& t : mp)
{
string frist = t.first;
int second = t.second;
if (second > res || (second == res && frist < p))
{
res = second;
p = frist;
}
}
cout << p << " " << res;
return 0;
}