1071 Speech Patterns (25 分)
 

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的更多相关文章

  1. A1071. Speech Patterns

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  2. PAT甲级——A1071 Speech Patterns

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  3. A1071 Speech Patterns (25 分)

    一.技术总结 开始拿到这道题目时,思考的是我该如何区分它们每一个单词,不知道这里还是要学习得知在cctype头文件中有一个函数用于查看是否为0~9.a~z.A~Z,就是isalnum(),又因为题目中 ...

  4. PAT_A1071#Speech Patterns

    Source: PAT A1071 Speech Patterns (25 分) Description: People often have a preference among synonyms ...

  5. 学习Java 以及对几大基本排序算法(对算法笔记书的研究)的一些学习总结(Java对算法的实现持续更新中)

    Java排序一,冒泡排序! 刚刚开始学习Java,但是比较有兴趣研究算法.最近看了一本算法笔记,刚开始只是打算随便看看,但是发现这本书非常不错,尤其是对排序算法,以及哈希函数的一些解释,让我非常的感兴 ...

  6. Pat1071: Speech Patterns

    1071. Speech Patterns (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming Peo ...

  7. 算法笔记--数位dp

    算法笔记 这个博客写的不错:http://blog.csdn.net/wust_zzwh/article/details/52100392 数位dp的精髓是不同情况下sta变量的设置. 模板: ]; ...

  8. 算法笔记--lca倍增算法

    算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...

  9. 算法笔记--STL中的各种遍历及查找(待增)

    算法笔记 map: map<string,int> m; map<string,int>::iterator it;//auto it it = m.begin(); whil ...

随机推荐

  1. Gym 101201G Maximum Islands (最大独立集)

    题意:给定一个图,L代表陆地,W代表水,C表示不确定,问你最多有多少岛. 析:首先给定的L周围必须是是W,只有这样才是最优的,因为如果是L,那么还得有另外的W来包围,不是最优的,那么剩下的就剩下C了, ...

  2. 递归,回溯和DFS的区别

    递归是一种算法结构,回溯是一种算法思想一个递归就是在函数中调用函数本身来解决问题回溯就是通过不同的尝试来生成问题的解,有点类似于穷举,但是和穷举不同的是回溯会“剪枝”,意思就是对已经知道错误的结果没必 ...

  3. javadoc tags

    Where Tags Can Be Used The following sections describe where the tags can be used. Note that these t ...

  4. HttpServletRequest介绍

    HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息. 二 ...

  5. Ubuntu 12.04 安装最新版本NodeJS

    昨天搭建了一个Windows NodeJS 运行环境,但Windows 运行NodeJS命令行各种别扭,开源包的编译也是各种问题,折磨了我一天一夜,果断换到Linux 平台.. 我选择了Ubuntu ...

  6. nutch-2.2.1 hadoop-1.2.1 hbase-0.92.1 集群部署(实用)

    原文地址: http://www.cnblogs.com/i80386/p/3540389.html 参考网站:http://blog.csdn.net/weijonathan/article/det ...

  7. 基于 Raphael 的 Web UI 设计 - 初稿

    基于 Raphael 的 UI 设计 - 初稿 还有很多地方需要完善,这次要把 Raphael 彻底用起来,^_^, 一定要150字哦,一图抵千字啊,^_^,不喜欢写字的,那么怎么凑够150字呢,说说 ...

  8. PostgreSQL递归查询

    原料 --创建组织架构表 create table "Org"( "OrgId" ) primary key, "ParentId" ), ...

  9. oracle中的序列,可以解决自增各种编号自动增长问题,说一下我的小问题。

    1.生成0001-9999的编码 //在PL/sql中 创建一个序列 Create sequence seq_cdptIncrement by 1 --自增数Start with 1 --开始累加数M ...

  10. 总结目前为止学到的关键字(break,continue,private,static,this,super,final,abstract)

    1.控制跳转语句:break(结束) 使用的场景: a.循环当中 b.switch break关键字需要注意的问题: 1.break关键字只能用于循环和switch语句当中,其本质就是结束整段语句的意 ...