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 ...
随机推荐
- KMeans聚类
常用的聚类方法: ①分裂方法: K-Means算法(K-平均).K-MEDOIDS算法(K-中心点).CLARANS算法(基于选择的算法) ②层次分析方法: BIRCH算法(平衡迭代规约和聚类).CU ...
- netcore mvc菜单,角色,权限
netcore mvc快速开发系统(菜单,角色,权限[精确到按钮])开源 AntMgr https://github.com/yuzd/AntMgr 基于netcore2.0 mvc 开发的 快速搭建 ...
- python 列表推导式 - python基础入门(16)
截止到目前为止,python基础内容已经学习了50%左右,在学习编程过程中,我们不仅要学习python语法,同时也需要学习如何把自己代码写的更美观,效率更高. 一.什么是推导式 推导式是从一个或者多个 ...
- Linux利器之perf(火焰图)
1 Ubuntu下安装perf 在Ubuntu18上默认没有安装perf,可以通过如下命令尝试:sudo apt-get install linux-tools 结果出来以下内容: Reading p ...
- Linux中查找最耗CPU的Java代码问题
第一步: 查看消耗CPU最高的进程PID [lolaage@web2 tomcat-ns]$ top top - 13:23:32 up 42 days, 19:11, 3 users, load ...
- 【CodeForces】868F. Yet Another Minimization Problem
原题链接 题目大意是有N个数,分成K段,每一段的花费是这个数里相同的数的数对个数,要求花费最小 如果只是区间里相同数对个数的话,莫队就够了 而这里是!边单调性优化边莫队(只是类似莫队)!而移动的次数和 ...
- Oracle的varchar2如何比较大小
首先要说的是Oracle中字符类型的比较都是基于ASCII码表来实现的,我就简单做个总结. Oracle中varchar2类型的字符串使用的是非填充空格的标准来进行比较的(表格中右边的那列,注意空格的 ...
- 数组、可变参数 、this关键字 (札记)
Thinking in java 读书笔记(P84 ~ P104) 作者:淮左白衣 写于:2018年4月10日16:42:57 目录 this 为什么可以代表调用对象 数组 数组中的 length 定 ...
- Markdown 空格详细介绍
它叫不换行空格,全称No-Break Space,它是最常见和我们使用最多的空格,大多数的人可能只接触了 ,它是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的 ...
- Centos7.3安装jenkins
安装jenkins 下载地址: https://jenkins.io/download/最新版本 将Jenkins上传到tomcat的webapp目录 ...