PAT甲级——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
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
string str, s = "", res;
map<string, int>words;
int maxN = -;
getline(cin, str);
for (int i = ; i < str.length(); ++i)
{
if(isalnum(str[i])!=)//数字也算
s += tolower(str[i]);
else if(s.length()>)
{
words[s]++;
s = "";
}
}
//注意,不要漏了最后一个单词
if (!s.empty())
words[s]++;
for (auto ptr = words.begin(); ptr != words.end(); ++ptr)
{
if (maxN < ptr->second)
{
maxN = ptr->second;
res = ptr->first;
}
}
cout << res << " " << maxN << endl;
return ;
}
PAT甲级——A1071 Speech Patterns的更多相关文章
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- 【算法笔记】A1071 Speech Patterns
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- A1071. Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- PAT Advanced 1071 Speech Patterns (25 分)
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 ...
- 1071 Speech Patterns——PAT甲级真题
1071 Speech Patterns People often have a preference among synonyms of the same word. For example, so ...
- PAT 1071 Speech Patterns[一般]
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For exam ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 03->OpenGL多边形,glut实现三角形条带和三角形扇
图形学中基本图元是多边形,一般要求是凸多边形,三角形是最简单的凸多边形,在图形渲染中比一般多边形其绘制速度快.今天学习OpenGL绘制三角形条带和三角形扇基础.编程环境! 1. 三角形条带 指定顶点序 ...
- 随笔记录 Linux基本操作命令 2019.7.27
临时关闭防火墙systemctl stop firewalld永久关闭防火墙systemctl disable firewalld 临时关闭selinux安全机制setenforce 0永久关闭sel ...
- css---7自定义字体
1.Adobe illustrator AI是一种应用于出版.多媒体和在线图像的工业标准矢量插画的软件,是一款非常好的矢量图形处理工具. 该软件主要应用于印刷出版.海报书籍排版.专业插画.多媒体图像处 ...
- springboot mail 发送邮件
新开发了一个新的功能,要求使用java发送邮件,在此记录下代码,以方便后来者: 1.首先需要开通邮箱,开通smtp功能,我这边使用的是新浪邮箱,试过163.qq,比较麻烦,后来看到别人使用新浪,直接使 ...
- Erlang学习记录:app demo
目录结构 │ Emakefile │ make.bat │ start.bat ├─config │ config.config │ server.app ├─ebin │ wulin_app.bea ...
- jQuery FormData使用方法
FormData的主要用途 将form表单元素的name与value进行组合,实现表单数据的序列化,从而减少表单元素的拼接,提高工作效率. 异步上传文件 注:FormData 对象的字段类型可以是 B ...
- Jmeter使用:JSON返回数据处理
想要解决的问题: 通过查询接口,获取response数据,作为下个请求post的参数值 后置处理器:JSON Extractor 先下载一个插件:JSONPathExtractor Names of ...
- csps退役记
AFO 省二稳了,指望文化课吧 hzoi加油
- (转)iframe 高度100%时,出现垂直滚动条
问题 需求是这样的,iframe在一个div中,并且iframe高度与div一样,所以设置了iframe高度是100%,结果div出现了滚动条,在排除了padding.margin的因素外,还是有滚动 ...
- csp-s模拟测试83(集训过半)
csp-s模拟测试83(集训过半) 一场信心赛.起初$OJ$爆了我就看见全场$A$了$T1$并且夹杂着$A$掉$T2$我就很难受. 这场比赛没有深入思考,很失败,一个多小时后就结束了我罪恶的一生. 0 ...