1071 Speech Patterns

大致题意就是找出一段话中,最常出现的单词。

注意点:

一,这里的单词由大小写字母和数字组成,坑死我了。

二,使用ctype库中的函数时,如果判断条件写成if(isupper(str[i]) == false),那么代码跑不动!必须写成if( !isupper(str[i]))(巨坑的语法)

STL:map(自排序+去重)

#include<iostream>
#include<map>
#include<cctype>
using namespace std;

int main() {
    string str,ans;
    getline(cin,str);
    map<string ,int> mp;
    for(int i = 0 ; i < str.length(); ++i) {
        while(i < str.length() && !isalnum(str[i])) ++i;//跳过所有非大小字母和数字
        string word;//这里的单词由大小写字母和数字组成
        while(i < str.length() && isalnum(str[i])) {
            if(isupper(str[i])) //大写字母转小写字母
                str[i]+=32;
            word += str[i];
            ++i;
        }
        if(word != "")
            mp[word]++;
    }
    int max = 0;
    for(auto it = mp.begin(); it != mp.end(); ++it) {
        if(max < it->second) {
            max = it->second;
            ans = it->first;
        }
    }
    cout<<ans<<" "<<max;
    return 0;
}

 

posted @ 2020-02-16 21:35  tangq123  阅读(160)  评论(0编辑  收藏  举报