#include <cstdio> #include <string> #include <iostream> #include <map> using namespace std; //被非字母数字分割开的所有 int main() { map<string, int> words; string line; getline(cin,line); string word; for(string::iterator it=line.begin();it!=line.end();it++){ if((*it>=‘a‘&&*it<=‘z‘)||(*it>=‘A‘&&*it<=‘Z‘)||(*it>=‘0‘&&*it<=‘9‘)){ if(*it>=‘A‘&&*it<=‘Z‘){ *it=‘a‘+(*it-‘A‘); } word.insert(word.end(),*it); } else if(!word.empty()){ words[word]++; word.clear(); } } if(!word.empty())words[word]++; int ma=0; string tmp; /*for(map<string,int>::iterator it=words.begin();it!=words.end();it++){ tmp = it->first; cout<<tmp<<" "<< it->second<<endl; }*/ for(map<string,int>::iterator it=words.begin();it!=words.end();it++){ if(it->second > ma){ ma=it->second; tmp=it->first; } } cout<<tmp<<" "<<ma<<endl; return 0; }
原文:https://www.cnblogs.com/flipped415/p/10351502.html