【算法笔记】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 ...
随机推荐
- oj1089-1096总结(输入输出练习)
//无限输出类 #include<stdio.h>int main(void){ int a,b; while((scanf("%d %d",&a,&b ...
- linux每天一小步---find命令详解
1 命令功能 find命令用于搜索指定目录下的文件,并配合参数做出相应的处理. 2 命令语法 find 搜索路径pathname 选项option [-exec -ok -print 执 ...
- Duplicate Symbol链接错误的原因总结和解决方法[转]
from:http://www.cocoachina.com/bbs/read.php?tid=177492 duplicate symbol是一种常见的链接错误,不像编译错误那样可以直接定位到问题的 ...
- window.location 对象
http://www.home.com:8080/windows/location/page.html?ver=1.0&id=timlq#love 1, window.location.hre ...
- handsontable-cell type
在单元格中呈现自定义的元素:不能使用html元素 var data = [ { title: "<a href='http://www.amazon.com/Professional- ...
- session喜欢丢值且占内存,Cookis不安全,用什么可以代替呢?
localstorage sessionstorage 在线资料 webdb 这些都是基于HTML5的新特性! 此外还可以使用服务器文件.DB等.
- Beginning Asp.Net Security 读书笔记-----XSS
几个月前通过Veracode对代码进行动态和静态安全扫描,扫出了数以千计的安全bug,基本上都是top 10的,安全漏洞. 其中CWE80,CWE601数量最多.具体CWE的定义可参考http://c ...
- 码农的福利来了, 编程在线Androd 客户端上线了
编程在线下载: 编程在线网站:http://codestudy.sinaapp.com (最新版2.1) 编程在线移动版:http://codestudy.sinaapp.com/mobile/ 编程 ...
- centos7 二进制安装mysql5.6
下载mysqltar包 wget -q http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.g ...
- 在Windows子系统(WSL)中配置开机启动服务
在WSL中跑了一些测试服务 比如 mysql nginx等,但关机后每次都要手动开启甚是吃力,本想着用rc.local来编辑开机启动 ,无奈不支持啊!先看看非WSL环境中是怎么实现的. 在 Ubunt ...