PAT Advanced 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
切记,在遍历的时候,要把最后一个数值加上去
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string,int> mmap;
string str;
getline(cin,str);
string tmp="";
for(int i=;i<str.length();i++) {
str[i]=tolower(str[i]);
if(isalnum(str[i])) tmp+=str[i];
else {
if(tmp!="") mmap[tmp]++;
tmp="";
}
}
if(tmp!="") mmap[tmp]++;
string res;int coun=;
for(auto it=mmap.begin();it!=mmap.end();it++)
if(it->second>coun){
coun=it->second;
res=it->first;
}
cout<<res<<" "<<coun;
return ;
}
PAT Advanced 1071 Speech Patterns (25 分)的更多相关文章
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- 【PAT甲级】1071 Speech Patterns (25 分)(getline(cin,x))
题意: 输入一行字符串,输出出现过次数最多的由字母和数字组成的字符串以及它出现的次数(对大小写不敏感,输出全部输出小写). AAAAAccepted code: #define HAVE_STRUCT ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT (Advanced Level) 1071. Speech Patterns (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- 1071 Speech Patterns (25)(25 分)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- PAT甲题题解-1071. Speech Patterns (25)-找出现最多的单词
分割字符串的用法+map映射给出input中出现次数最多的单词,如果次数相同,给出按字典序最小的. 这里我用了自定义分隔符来读取字符串,方法如下: //按照定义的分隔符d来分割字符串,对str进行读取 ...
- 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 Advanced 1154 Vertex Coloring (25 分)
A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices ...
随机推荐
- Python3多重继承排序原理(C3算法)
参考:https://www.jianshu.com/p/c9a0b055947b https://xubiubiu.com/2019/06/10/python-%E6%96%B9%E6%B3%95% ...
- 如何调试Maven软件的源代码
和调试maven插件方法一样 修改maven源代码 打包模块apache-maven,生成apache-maven-x.x.x-bin.tar.gz 解压上面的压缩包,生成目录apache-maven ...
- jenkins:从FTP服务器下载文件
lftp 账号:密码@192.168.207.2 lcd /home/eccore/app/chen get -c /基础运维共享文件/OK-TeamViewer14.2.2558.rar
- 【VS开发】【miscellaneous】 Windows下配置Git
[转自]http://blog.csdn.net/exlsunshine/article/details/18939329 1.从git官网下载windows版本的git:http://git-scm ...
- weblogic搭建总结
目录: 一.安装weblogic软件 二.创建域 三.启动管理节点 四.创建被管理节点 五.部署应用 一.安装weblogic软件 一.关闭selinux和防火墙 service iptables s ...
- Hbuilder连接苹果手机
最简单的连接方法 1 手机电脑连接---usb 2 pc下载iTunes 3 信任电脑,手机信任设备(设置--->通用--->设备管理-->信任) 4 ...
- docker(学习笔记)
# 1. Docker介绍## 1.1 什么是容器?## 1.2 容器的前世今生FreeBASE jail ------> Linux vserverchroot ----> 完整的根文件 ...
- C++ 编译错误 jump to case label [-fpermissive]
<花的微笑>--- 钢琴曲,石进 今天再用C++写代码时,出现了编译错误 jump to case label [-fpermissive] 原因:使用switch语句时,再case中定义 ...
- 解决无/var/log/messages 问题
转载于:https://blog.csdn.net/C_Major/article/details/51321684 1 内核编程insmod后,Ubuntu查看日志无/var/log/message ...
- SQLServer 查询view中是否包含某个关键字
在数据库view的创建中,会遇到一些跨数据库的view脚本,但是在将view更新到production的时候可能忘记更改database name,导致出现一些问题. 以下脚本可以检查出包含某个关键字 ...