#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
using namespace std;
map<string,int> mp;
bool isc(char a)
{
if(a>='a'&&a<='z')return true;
else if(a>='A'&&a<='Z')return true;
else if(a>='0'&&a<='9')return true;
else return false;
}
void tolower(string &str)
{
for(int i=0;i<str.length();i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
str[i]=str[i]-'A'+'a';
}
}
}
string maxstr;
int maxn=-1;
void split(string str)
{
string temp="";
int index=0;
int pre=0;
while(index<str.length())
{
if(!isc(str[index])&&index==pre)
{
pre++;
index++;
}
else if(!isc(str[index])&&index>pre)
{
temp=str.substr(pre,index-pre);
tolower(temp);
if(mp.find(temp)==mp.end())
{
mp[temp]=0;
}
mp[temp]++;
index++;
pre=index;
if(mp[temp]>=maxn)
{
if(mp[temp]>maxn)
{
maxn=mp[temp];
maxstr=temp;
}
else if(temp<maxstr)
{
maxstr=temp;
}
}
}
else if(isc(str[index])&&index<str.length()-1)
{
index++;
}
else if(isc(str[index])&&index==str.length()-1)
{
temp=str.substr(pre,index-pre+1);
tolower(temp);
if(mp.find(temp)==mp.end())
{
mp[temp]=0;
}
mp[temp]++;
index++;
pre=index;
if(mp[temp]>=maxn)
{
if(mp[temp]>maxn)
{
maxn=mp[temp];
maxstr=temp;
}
else if(temp<maxstr)
{
maxstr=temp;
}
}
}
}
}
int main()
{
string str;
getline(cin,str);
split(str);
cout<<maxstr<<" "<<maxn<<endl;
return 0;
}
PAT甲1071 Speech Patterns(25 分)
最新推荐文章于 2020-08-12 21:50:17 发布