clipboard.png
唯一要注意的是当读入字符串进行有效字符截取的时候,队尾可能还会有有效字符,记得在循环结束的时候进行word的判别;

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<map>
#include<string>
using namespace std;
using std::map;
bool check(char c){
    if(c>='a'&&c<='z')
        return true;
    if(c>='A'&&c<='Z')
        return true;
    if(c>='0'&&c<='9')
        return true;
    else
        return false;
}
map<string,int>count_;
int main(){
    string str;
    getline(cin,str);
    string word="";
    for(int i=0;i<str.size();i++){
        if(check(str[i])){
            if(str[i]>='A'&&str[i]<='Z'){
                str[i]+=32;
            }
            word+=str[i];
        }else{
            if(word!=""){
                if(count_.find(word)==count_.end()){
                    count_[word]=1;
                }else{
                    count_[word]++;
                }
                word="";
            }
        }
    }
    if(word!=""){
        if(count_.find(word)==count_.end()){
            count_[word]=1;
        }else{
            count_[word]++;
        }
    }
    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;
    system("pause");
    return 0;
}

宋霖轩
16 声望4 粉丝

克哈的霓虹都为我闪烁


« 上一篇
PAT A1054
下一篇 »
PAT A1022