1071. Speech Patterns (25)

来源:互联网 发布:成都工作 知乎 编辑:程序博客网 时间:2024/06/16 12:07

用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);        }}
0 0
原创粉丝点击