【PAT甲级】1071 Speech Patterns (25 分)(getline(cin,x))

题意:

输入一行字符串,输出出现过次数最多的由字母和数字组成的字符串以及它出现的次数(对大小写不敏感,输出全部输出小写)。

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 string word;
 5 map<string,int>mp;
 6 string s;
 7 int main(){
 8     ios::sync_with_stdio(false);
 9     cin.tie(NULL);
10     cout.tie(NULL);
11     string x;
12     getline(cin,x);
13     int n=x.size();
14     for(int i=0;i<n;++i)
15         if((x[i]>='A'&&x[i]<='Z')||(x[i]>='a'&&x[i]<='z')||(x[i]>='0'&&x[i]<='9')){
16             if(x[i]>='A'&&x[i]<='Z')
17                 x[i]=x[i]-'A'+'a';
18             word.push_back(x[i]);
19         }
20         else if(word!=""){
21             ++mp[word];
22             word.clear();
23         }
24     if(word!=""){
25         ++mp[word];
26         word.clear();
27     }
28     int mx=0;
29     for(auto it:mp)
30         if(it.second>mx){
31             mx=it.second;
32             s=it.first;
33         }
34     cout<<s<<" "<<mx;
35     return 0;
36 }

 

 

 

posted @ 2019-11-05 06:38  sewage  阅读(142)  评论(0编辑  收藏  举报