#include <bits/stdc++.h> using namespace std; bool check(char c)//检查是否为字母或数字 { if(c>=‘A‘&&c<=‘Z‘||c>=‘a‘&&c<=‘z‘||c>=‘0‘&&c<=‘9‘) return true; else return false; } int main() { map<string,int> count; string str; getline(cin,str); int i = 0; while(i<str.length()) { string word; while(i<str.length()&&check(str[i])) { if(str[i]>=‘A‘&&str[i]<=‘Z‘){ //大写统一改为小写 str[i] += (‘a‘-‘A‘); } word += str[i]; //字符拼接 + i++; } if(word != ""){ if(count.find(word)==count.end()) count[word]=1; //这单词还没出现过,计数为1 else count[word]++; } while(i<str.length()&&!check(str[i])) //遇到其它字符跳过 { i++; } } string ans; int MAX=0; for(map<string,int>::iterator it=count.begin();it != count.end();it++) //只能用!=,而不能用<end() { if(it->second>MAX) { ans = it->first; MAX = it->second; } } cout<<ans<<" "<<MAX<<endl; return 0; }
原文地址:https://www.cnblogs.com/qiangz/p/8552382.html
时间: 2024-10-25 02:41:56