PAT 甲级 A1071

#include<iostream>
#include<string>
#include<map>
using namespace std;
bool check(char c) {    //  检查字符是否是 [0-9 A-Z a-z]. 
    if(c >= '0' && c <= '9') return true;
    if(c >= 'A' && c <= 'Z') return true;
    if(c >= 'a' && c <= 'z') return true;
    return false;
}
int main() {
    map<string, int> count;        //  count计数字符串出现的次数
    string str;
    getline(cin, str);        //  读入整行字符串 
    int i = 0;        //  定义下标 
    while(i < str.length()) {    //  在字符串范围内 
        string word;    //  单词 
        while(i < str.length() && check(str[i]) == true) {    //  如果是单词的字符 
            if(str[i] >= 'A' && str[i] <= 'Z') {
                str[i] += 32;    //  大写字母转小写 
            }
            word += str[i];    //  单词末尾添加该字符 
            i++;    //  下标后移一位 
        }
        if(word != "") {    //  单词非空,令次数加一 
            if(count.find(word) == count.end()) 
                count[word] = 1;
            else 
                count[word]++;
        }
        while(i < str.length() && check(str[i]) == false) {
            i++;    //  跳过非单词字符 
        }
    }
    string ans;    //  存放出现次数最多的单词 
    int MAX = 0;    //  出现最多的单词的次数 
    for(map<string, int>::iterator it = count.begin(); it != count.end(); it++) {
        if(it->second > MAX) {    //  寻找出现次数最多的单词 
            MAX = it->second;
            ans = it->first;
        }
    }
    cout << ans << " " << MAX << endl;        //  输出结果 
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zjsaipplp/p/10424850.html