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. python 3 print function

    if episode % 50 == 0: print('Episode {} Total Reward: {} counter: {}'.format(episode,G,counter))

  2. 全球顶尖大学的UX课程资源,英文!

    本文由MOCKPLUS团队原创,转载请标明出处,原型设计工具就用更快.更简单的mockplus 要想成为一名优秀的UX设计师,需要掌握很多必备技能.比如,掌握用户体验设计的所有知识和信息架构(易用性方 ...

  3. 从iOS 11 UI Kit中谈谈iOS 11的新变化

    北京时间9月20日凌晨1点,iOS 11终于迎来了正式版的推送,相信各位小伙伴已经在第一时间进行了升级.iOS 11毫无疑问是一次大规模的系统更新,UI.系统内核.锁屏等多方面都进行了不同程度的改进. ...

  4. webform调用windows服务

    准备工作: .电脑->管理->本地用户和组->组->Administrator双击->隶属->添加Network service->确定 .启动windows ...

  5. linux网络编程模型

    1.编程模型 Linux网络编程模型是基于socket的编程模型

  6. 解决win7和2008连接windows 2003远程桌面很卡的问题

    解决win7和2008连接windows 2003远程桌面很卡的问题 来源:http://www.hack1990.com/ 作者:佚名 时间:2013-04-12 TAG: 我要投稿 原因在于从vi ...

  7. 关于anroid设置webview背景方法探讨(转)

    最近的项目中一直关于webView设置背景色问题在研究,最终找到了解决的方法. 基于我项目的需求,从服务端传过来的是带有标签的文本,如果使用textView会让整个布局显得很乱,里面的<img ...

  8. Shell脚本传递带有空格的参数[摘录自网络]

    参数处理 说明 $# 传递到脚本的参数个数 $* 以一个单字符串显示所有向脚本传递的参数 $$ 脚本运行的当前进程ID号 $! 后台运行的最后一个进程的ID号 $@ 与$#相同,但是使用时加引号,并在 ...

  9. 深入浅出CSS:Div(一)

    这个系列是学习笔记,简明记录结论性的知识. 新建一个层时,border为零,margin为0,padding为0,如果不指定宽度(width),则自动100%填充父元素. 三.层与父元素的关系 1. ...

  10. 2016-2017 《移动平台开发》实验三 敏捷开发与XP实践

    概述 软件=程序+软件工程 软件企业=软件+商业模式 XP是一种轻量(敏捷).高效.低风险.柔性.可预测.科学而且充满乐趣的软件开发方式. 在更短的周期内,更早地提供具体.持续的反馈信息. 在迭代的进 ...