模拟题
1: map默认就是按照key排序的
2:添加元素时,可以直接map[string]++;
#include <stdio.h>
#include <string>
#include <iostream>
#include <map>
using namespace std;
map<string,int> m;
int main(){
freopen("in.txt","r",stdin);
char c;
string str;
bool ignoreNonCha = true;//首位 或者 前一个字符为非字符数字时,不用将str放进map
while(scanf("%c",&c)!=EOF){
if((c >= '0' && c <='9')||(c >= 'a' && c <='z')||(c >= 'A' && c <='Z')){
if(c >= 'A' && c<= 'Z'){
c = c-'A'+'a';
}
str += c;//string后面直接加char
if(ignoreNonCha){ //已经有合法字符了,那么下一个非法字符出现时,就要把str放进map
ignoreNonCha = false;
}
}else if(!ignoreNonCha){
m[str]++;//map里没有str也可以,实在是太人性化了
str.clear();
ignoreNonCha = true;
}
if(c=='\n'){//必须放末尾,否则可能导致最后一个string没有放进map
break;
}
}
string maxWord;
int max = 0;
for(map<string,int>::iterator it = m.begin(); it != m.end(); it++){//不需要按value排序,只要找到最大的value就行噻
if(it->second > max){
maxWord = it->first;
max = it->second;
}
//test
//cout<<it->first<<" "<<it->second<<endl;
}
cout<<maxWord<<" "<<max<<endl;
return 0;
}