难受,各种情况都试了,实在找不到哪里错了
#include <cstdio>
#include <iostream>
#include <string>
#include <map>
using namespace std;
map<string,int> m;
bool ischar(char c){
if((c>='0'&&c<='9') || (c>='a'&&c<='z') || (c>='A'&&c<='Z')){
return true;
}
return false;
}
int main(){
string s;
getline(cin,s);
int len = s.size();
int begin=-1, end=-1;
for(int i=0; i<len; i++){
if((ischar(s[i])&&i==0) || (ischar(s[i])&&!ischar(s[i-1]))){
begin = i;
}
if((ischar(s[i])&&i==len-1) || (ischar(s[i])&&!ischar(s[i+1]))){
end = i;
}
if(begin!=-1 && end!=-1){
string sub = s.substr(begin, end-begin+1);
if(sub[0]>='A' && sub[0]<='Z'){
sub[0] = sub[0] + 32;
}
if(sub.length() != 0) m[sub]++;
begin = -1;
end = -1;
}
}
int max = m.begin()->second;
string temp = m.begin()->first;
for(map<string,int>::iterator it=m.begin(); it!=m.end(); it++){
if(it->second == max){
if(it->first < temp){
max = it->second;
temp = it->first;
}
} else if(it->second > max){
max = it->second;
temp = it->first;
}
}
cout << temp << " " << max;
return 0;
}