PAT_1071. Speech Patterns
来源:互联网 发布:苏堤漫步简介知乎 编辑:程序博客网 时间:2024/06/05 05:05
#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
阅读全文
0 0
- PAT_1071. Speech Patterns
- Speech Patterns
- 1071. Speech Patterns (25)
- 1071. Speech Patterns (25)
- 1071. Speech Patterns
- 1071. Speech Patterns (25)
- 1071. Speech Patterns
- PAT 1071. Speech Patterns
- 1071. Speech Patterns
- 1071. Speech Patterns (25)
- PAT 1071 Speech Patterns
- PAT 1071. Speech Patterns
- 1071. Speech Patterns (25)
- 1071. Speech Patterns (25)
- 1071.Speech Patterns
- 1071. Speech Patterns (25)
- 1071. Speech Patterns (25)
- 1071. Speech Patterns
- MySQL在Linux下Shell脚本操作命令
- 读取文件截取字符串 写成c数组
- 人脸识别
- Cookie帮助类
- Maven 搭建私服(nexus)
- PAT_1071. Speech Patterns
- Java基础学习之前—我是如何踏上Java这条路的
- Java的native方法
- redis笔记
- php
- MySQL性能优化的21个最佳实践
- 超平面是什么?——理解超平面(SVM开篇之超平面详解)
- 使用html2canvas实现浏览器截图
- 贪婪算法