问题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805398257647616
题意:
令"单词"的定义为大小写字母,数字的组合。给出一个字符串,问出现次数最多的单词及其出现次数(一起除大小写字母和数字外的字符都作为分隔符)。其中字母不区分大小写,且最后按小写字母输出。
Note:
1.读入整行字符串getline(cin, str)
2. 大写字母转小写字符在原字符上+32
#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
bool check(char s){
if(s >= 'a' && s <= 'z')
return true;
if(s >= 'A' && s <= 'Z')
return true;
if(s >= '0' && s <= '9')
return true;
return false;
}
int main(){
string str;
getline(cin, str);//读入一整行
map<string, int> mp;
int i = 0;
while(i < str.length()){
string word;
while(i < str.length() && check(str[i]) == true){
if(str[i] >= 'A' && str[i] <= 'Z')
str[i] += 32;
word += str[i];
i++;
}
if(word != ""){
if(mp.find(word) != mp.end())
mp[word] ++;
else
mp[word] = 1;
}
while(i < str.length() && check(str[i]) == false)
i++;
}
string MAX;
int MAXK = 0;
for(map<string, int>::iterator it = mp.begin(); it != mp.end(); it++){
if(it->second > MAXK){
MAX = it->first;
MAXK = it->second;
}
}
cout << MAX <<" "<< MAXK;
return 0;
}