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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- Window下,前后端分离项目,登录权限验证中的,Redis相关操作
[1]官网下载Redis(解压版) https://redis.io/download [2]切换到目录下打开DOS,执行指令启动Redis redis-server.exe redis.window ...
- JS事件 内容选中事件(onselect)选中事件,当文本框或者文本域中的文字被选中时,触发onselect事件,同时调用的程序就会被执行。
内容选中事件(onselect) 选中事件,当文本框或者文本域中的文字被选中时,触发onselect事件,同时调用的程序就会被执行. 如下代码,当选中用户文本框内的文字时,触发onselect 事件, ...
- 随笔记录 linux命令 2019.7.29
系统命令 一. type 查看命令是内部命令还是内部命令 help 帮助 man 在线帮助 cd 切换目录 pwd 查看所在路径 stat 查看文件详细信息 ls ...
- 用多线程发送邮箱(一次给一个用户发送N封邮件)
前台不用写,后台执行方法就可以了. namespace SendMail { public partial class SendMail_Page : System.Web.UI.Page { pro ...
- 93. 复原IP地址
题目描述: 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135&q ...
- poj1363 Rails Central Europe 1997
P.S.: 输出换行 三个方法 1.直接按照要求做 根据给的数,需要push,pop哪些数据,具有唯一性 数最多进栈一次,出栈一次 O(n) Source Code Problem: User: co ...
- day30 python类的继承,抽象类等
Python之路,Day17 = Python基础17-面向对象入门 继承 class Student(People): pass print(Student.__bases__) # 查看 Stud ...
- (组合数学)不定方程的解+猜测——cf997B
首先要求出三种等价情况 5×1+1×50=1×5+5×105×1+1×50=1×5+5×10 9×5=5×1+4×10 8×5+1×50=9×10 那么可以求出三种关于x5,x10的不可行条件 x ...
- 数论剩余系——cf1089F
关于模和互质,很好的题目 /* n两个质因子 x,y有 ax+by=n-1 ax+by=n-1 ax+1+by=n y|ax+1 gcd(x,y)=1 ax%y,a取[1,y-1],就会有[1,y-1 ...
- Exception from HRESULT:
在MFC工程中,在类向导的时候,偶尔会遇到 "Exception from HRESULT:" 的问题,问题的原因可能是移动工程之类的操作破坏了工程的某些文件或者更改了某些路径的映 ...