[PAT] Class 1071 Speech Patterns (25 points) (getline (cin, x))

Meaning of the questions:

Line input string, the number string output over most of letters and numbers, and the number of times it appears (case insensitive, outputs of all the output lowercase).

Code:

#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;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11796091.html