【算法笔记】A1071 Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "the police", while others may prefer "the cops". Analyzing such patterns can help to narrow down a speaker's identity, which is useful when validating, for example, whether it's still the same person behind an online avatar.
Now given a paragraph of text sampled from someone's speech, can you find the person's most commonly used word?
Input Specification:
Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return \n
. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z
].
Output Specification:
For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a "word" is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.
Note that words are case insensitive.
Sample Input:
Can1: "Can a can can a can? It can!"
Sample Output:
can 5
题意
统计一句话中出现次数最多的单词,输出这个单词和出现次数。注意can1也算一个单词。
思路
步骤1:把每个单词从句子里分离出来。
步骤2:用map记录出现次数。
步骤3:遍历map,输出次数最多的单词。
code:
#include<bits/stdc++.h>
using namespace std;
map<string, int> wordStat;
bool check(char c){
if(c>=''&&c<='') return true;
if(c>='A'&&c<='Z') return true;
if(c>='a'&&c<='z') return true;
return false;
}
int main(){
string str, pattern;
int i = , max = ;
getline(cin, str);
while(i < str.length()){
string word;
while(i < str.length() && check(str[i])==true){
if(str[i]>='A'&&str[i]<='Z') str[i] += ;
word += str[i];
i++;
}
if(word != ""){
if(wordStat.find(word) == wordStat.end()) wordStat[word] = ;
else wordStat[word]++;
}
while(i < str.length() && check(str[i])==false){
i++;
}
}
for(map<string, int>::iterator it = wordStat.begin(); it!=wordStat.end(); it++){
if(max <= it->second){
max = it->second;
pattern = it->first;
}
}
cout<<pattern<<" "<<max;
return ;
}
【算法笔记】A1071 Speech Patterns的更多相关文章
- A1071. Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- PAT甲级——A1071 Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- A1071 Speech Patterns (25 分)
一.技术总结 开始拿到这道题目时,思考的是我该如何区分它们每一个单词,不知道这里还是要学习得知在cctype头文件中有一个函数用于查看是否为0~9.a~z.A~Z,就是isalnum(),又因为题目中 ...
- PAT_A1071#Speech Patterns
Source: PAT A1071 Speech Patterns (25 分) Description: People often have a preference among synonyms ...
- 学习Java 以及对几大基本排序算法(对算法笔记书的研究)的一些学习总结(Java对算法的实现持续更新中)
Java排序一,冒泡排序! 刚刚开始学习Java,但是比较有兴趣研究算法.最近看了一本算法笔记,刚开始只是打算随便看看,但是发现这本书非常不错,尤其是对排序算法,以及哈希函数的一些解释,让我非常的感兴 ...
- Pat1071: Speech Patterns
1071. Speech Patterns (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming Peo ...
- 算法笔记--数位dp
算法笔记 这个博客写的不错:http://blog.csdn.net/wust_zzwh/article/details/52100392 数位dp的精髓是不同情况下sta变量的设置. 模板: ]; ...
- 算法笔记--lca倍增算法
算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...
- 算法笔记--STL中的各种遍历及查找(待增)
算法笔记 map: map<string,int> m; map<string,int>::iterator it;//auto it it = m.begin(); whil ...
随机推荐
- 前端福利之jQuery文字轮播特效(转)
闲谈:离开学校那座象牙塔已经也有大半年的事件了,生活中不再充满了茫然只有忙碌.连续加班加点大半个月,做的活动项目终于算是告一段落了,而今天也将是考验其真正价值的时候,现在将这次开发中遇到的问题做一下总 ...
- Linux下的用户权限
用户权限: drwxr-xr-x root root - : boot #权限位 硬连接数 所属用户 所属组 大小 最后修改时间 文件/目录# r w x 4 2 1 用户权限位分为3段,分别对应US ...
- CSS实现图片阴影效果
<title>无标题文档</title> <style type="text/css"> /*方法一:使用一个GIF文件的方法*/ .gifsh ...
- Swift:使用CAShapeLayer打造一个ProgresssBar
ProgressBar是一个很小却在很多地方都会用到的东西.也许是网络连接,也许APP本身有很多东西需要加载的.默认的只有一个旋转的菊花,对于打造一款个性的APP这显然是不够的.这里就使用CAShap ...
- 关于SQLServer无法对数据库'XXX'执行删除,因为它正用于复制。错误:'3724' 的解决方案
关于这个错误,是因为在服务器上想把数据库复制到本地,使用了“发布.订阅”方案,结果后来没成功,删除本地数据库的时候出现了这个错误,说“无法对数据库'XXX'执行删除,因为它正用于复制”. 解决方案:只 ...
- koa学习笔记
卸载node http://www.it165.net/os/html/201504/12427.html 安装 sudo npm install -g n sudo n stable 装个稳定版试试 ...
- 解决<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 过长
解决<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 过长 <i ...
- 利用反射(Reflection)处理对象
创建一个学生类: public class Student { public int Id { set; get; } public string Name { set; get; } public ...
- FIM控制同步用户
C:\Program Files\Microsoft Office Servers\15.0\Synchronization Service\UIShell 这个路径下,你如果懂FIM,可以进去看看 ...
- HTML5 Communication API
本文探讨用于构建实时(real-time)跨域(cross-origin)通信的两个重要模块:跨文档消息通讯和XMLHttpRequest Level 2.通过它们,我们可以构建引人注目的Web应用, ...