#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
char s[1050000];
string ans[1050000],temp;
int tot=0;
int main()
{
gets(s);
//预处理
for(int i=0;i<1050000;i++)
if(s[i]>='A'&&s[i]<='Z')
s[i]=s[i]-'A'+'a';
//分词
for(int i=0;s[i]!=0;i++)
{
if((s[i]>='0'&&s[i]<='9')||(s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z'))
temp+=s[i];
else if(temp!="")
{
ans[tot++]=temp;
temp="";
}
}
if(temp!="")
ans[tot++]=temp;
//统计频率最高的词汇
sort(ans,ans+tot);
int pre=0,now=0,appearTime=0;
string highWord;
while(1)
{
if(ans[now]==ans[pre])
now++;
else
{
if(now-pre>appearTime)
{
appearTime=now-pre;
highWord=ans[pre];
}
pre=now;
}
if(now==tot)
{
if(now-pre>appearTime)
{
appearTime=now-pre;
highWord=ans[pre];
}
pre=now;
break;
}
}
cout<<highWord<<" "<<appearTime<<endl;
return 0;
}
思路:
1.输入字符串保存在char型数组中
2.对数组进行预处理,将大写字母转换成小写字母
3.将数组中的每个"word"都保存在一个string数组中
4.对这个string数组按字典序排序(使得所有相同的"word"相邻排列)
5.统计出现次数最多的word