每个字符串的题都能学到新东西。。。
不擅长字符串处理的,去看网上的代码,学到一堆新的函数 传送门
这题用isalnum()函数就极其简单。
还有个注意点,第一个循环中的第二个if,不能用else if,这样导致字符串最末是非法字符的case过不了,测试数据最后一个case应该就是卡了这个点。
#include<bits/stdc++.h>
using namespace std;
int n,d;
map<string,int> statistic;
int main(){
string s, tmp;
getline(cin,s);
for(int i = 0; i < s.length(); ++i){
if(isalnum(s[i])){
tmp += tolower(s[i]);
}
if(!isalnum(s[i]) || i == s.length() - 1){
if(tmp.length() > 0) statistic[tmp]++;
tmp = "";
}
}
int maxcnt = 0;
string ans;
for(map<string,int>::iterator it = statistic.begin(); it != statistic.end(); ++it){
if(it->second > maxcnt){
maxcnt = it->second;
ans = it->first;
}
}
cout << ans << " " << maxcnt <<endl;
return 0;
}