AcWing 1557. 说话方式
原题链接
简单
作者:
林小鹿
,
2020-11-04 18:52:32
,
所有人可见
,
阅读 347
C++ 代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<unordered_map>
using namespace std;
bool check(char c)
{
if(c>='0'&&c<='9') return true;
if(c>='a'&&c<='z') return true;
if(c>='A'&&c<='Z') return true;
return false;
}
char to_lower(char c)
{
if (c>='A'&&c<='Z') c+=32;
return c;
}
int main()
{
string str;
getline(cin,str);
unordered_map<string,int>hash;
for(int i=0;i<str.size();i++)
{
int j=i;
if(check(str[i]))
{
string word;
while(j<str.size()&&check(str[j]))
{
word+=to_lower(str[j]);
j++;
}
i=j;
hash[word]++;
}
}
string word;
int cnt=-1;
for(auto c: hash)
{
if(c.second>cnt||c.second==cnt&&c.first<word)
{
word=c.first;
cnt=c.second;
}
}
cout<<word<<' '<<cnt<<endl;
return 0;
}