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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 【23. 合并K个排序链表】【困难】【优先队列/堆排序】
合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6] 输出: 1->1-> ...
- 数据库MySQL--修改数据表
创建数据库::create database 数据库名: 如果数据不存在则创建,存在不创建:Create database if not exists 数据库名 ; 删除数据库::drop datab ...
- canvas绘制线和矩形
###canvas绘制矩形 HTML中的元素canvas只支持一种原生的图形绘制:矩形.所有其他的图形的绘制都至少需要生成一条路径 1.绘制矩形 canvas提供了三种方法绘制矩形: ----> ...
- 【JZOJ6288】旋转子段
description analysis 可以先用前缀和把原串不调整的方案数先求出来 对于一种翻转,肯定是把\([i..a[i]]\)或\([a[i]..i]\)这段区间翻转 也可以看做是以\({i+ ...
- 【JZOJ6271】锻造 (forging)
description analysis 首先看一下\(p=1\),即\(1\)以后的合成一定成功的情况 如果按照求期望值的一般做法求两把\(0\)合成\(1\)的期望,会画出一棵无穷大的树 这个的期 ...
- day25 模块,sys, logging, json, pickle
Python之路,Day13 = Python基础13 sys模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sy ...
- npm run server报错
从git上clone的vue项目npm install后npm run server报错 $ npm run dev > lufei@1.0.0 dev E:\pythonProject\luf ...
- Android中visibility属性
Android开发中,大部分控件都有visibility这个属性,其属性有3个分别为“visible ”.“invisible”.“gone”.主要用来设置控制控件的显示和隐藏. 1) 可见(visi ...
- servlet的抽取
servlet的抽取 servlet按照模块来划分,比如注册和登录的servlet就放到user的servlet中 原来: 登录时登录的servlet 注册时注册的servlet 现在: 登录注册的s ...
- 汽车超人CEO郑超:不惧资本寒冬 融资已增至57亿
汽车超人CEO郑超:不惧资本寒冬 融资已增至57亿 来源:互联网 时间:2017-06-01 阅读:3次 篇一 : 汽车超人CEO郑超:不惧资本寒冬 融资已增至57亿 近日记者获悉,金固股份旗 ...