AcWing 1557. 说话方式
原题链接
简单
作者:
断剑悲歌
,
2024-01-01 21:52:23
,
所有人可见
,
阅读 1
#include <iostream>
#include <unordered_map>
using namespace std;
bool checked(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') return c + 32;
return c;
}
int main(){
string str;
getline(cin , str);
unordered_map<string,int> hash; //定义一个map类型的哈希表
for(int i = 0;i < str.size();i++){
if(checked(str[i])){
int j = i; //双指针算法
string word;
while(j < str.size() && checked(str[j])) word += to_lower(str[j++]);
hash[word]++;
i = j;
}
}
string res;
int cnt = -1;
for(auto item : hash){
if(item.second > cnt || item.second == cnt && item.first < res){
res = item.first;
cnt = item.second;
}
}
cout << res << ' ' << cnt << endl;
return 0;
}