首页 > 学院 > 开发设计 > 正文

1071. Speech Patterns (25)

2019-11-08 02:54:40
字体:
来源:转载
供稿:网友

用map保存Word及次数,由于map自动按字典序保存,最后只要输出第一个和max一样的字符就可以了

#include<iostream>#include<string>#include<vector>#include<map>#include<cctype>#include<algorithm>#PRagma warning(disable:4996)using namespace std;int main(){ string str; getline(cin, str); map<string, int> all; string temp; int max = 0; for (auto x : str)//处理字符,并计算出最大出现次数 { if ((x >= '0'&&x <= '9') ||//对于0-9,a-z,A-Z的字符增加在当前temp末尾(小写) (x >= 'a'&&x <= 'z') || (x >= 'A'&&x <= 'Z')) temp += tolower(x); else if (!temp.empty())//对于非以上字符,判断temp是否为空字符,若不为空,保存 { if (++all[temp] > max) max = all[temp]; temp=""; } } if(!temp.empty()) if (++all[temp] > max) max = all[temp];//处理最后剩余的字符 for (auto it = all.begin();it != all.end();it++)//输出 if (it->second == max) { printf("%s %d/n", it->first.c_str(), it->second);exit(0); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表