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

题意:

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

代码:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
string word;
map<string,int>mp;
string s;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string x;
getline(cin,x);
int n=x.size();
for(int i=0;i<n;++i){
if((x[i]>='A'&&x[i]<='Z')||(x[i]>='a'&&x[i]<='z')||(x[i]>='0'&&x[i]<='9')){
if(x[i]>='A'&&x[i]<='Z')
x[i]=x[i]-'A'+'a';
word.push_back(x[i]);
}
else if(word!=""){
++mp[word];
word.clear();
}
}
if(word!=""){
++mp[word];
word.clear();
}
int mx=0;
for(auto it:mp){
if(it.second>mx){
mx=it.second;
s=it.first;
}
}
cout<<s<<" "<<mx;
return 0;
}

猜你喜欢

转载自www.cnblogs.com/ldudxy/p/11796091.html
今日推荐