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 ...
随机推荐
- swift ClassNameFromString 的替换方法 + 创建TableviewHelper
1. ClassNameFromString 的方法oc 可以正常使用,但是swift 不能直接使用的,下面的代码 func getAPPName() -> String?{ let nameK ...
- 好工具必须SHOW出来! NGFW下一代防火墙性能评估利器:Safire !
2019-09-26 00:05:54 今天先起个头,后面陆续完善 NGFW下一代防火墙是什么? 我们要关注NGFW下一代防火墙的哪些指标? 为什么说NGFW的性能不好评估?现有的评估手段工具介绍? ...
- 欧姆龙_NX1P_PLC功能模块_限定安全作业空间
程序 和说明 以及流程图 已上传到百度云盘 https://pan.baidu.com/s/1kYF11pQHJ4VxyaXr8E_gAA 主要是用来向量的叉乘 判断俩向量正向夹角
- sklearn.svc 参数
sklearn.svc 参数 sklearn中的SVC函数是基于libsvm实现的,所以在参数设置上有很多相似的地方.(PS: libsvm中的二次规划问题的解决算法是SMO). 对于SVC函数的参数 ...
- 使用expect实现自动交互,shell命令行自动输入,脚本自动化,变量引用,expect spawn执行带引号命令,expect 变量为空,不生效,不能匹配通配符*,函数,数组
背景 有需求,在允许命令或者脚本跳出交互行,需要进行内容输入,但需要人手动输入,不是很方便,此时可以通过expect来实现自动互动交互. expect是一个自动交互功能的工具,可以满足代替我们实际工作 ...
- 纯小白安装MongoDB的图形界面工具adminMongo
今天安了两个MongoDB的图形界面工具,robot3和adminMongo,至于为什么安两个....因为网上说啥好用的都有,我也很迷... 安装adminMongo的时候...和正常软件安装流程不太 ...
- shell将txt转换为csv
cat aa.txt | tr -s '[:blank:]' ',' > bb.csv
- 乐字节Java学习01-Java语言概述,Java发展前景,Java的特点
从这篇文章开始,小乐准备写一整系列的Java学习文章,一步一步带您进入Java的神奇世界! 这是第一篇:Java语言概述以及Java的发展前景 1.SUN公司介绍 SUN公司全称是Stanford U ...
- [转帖]MMU内存管理单元
MMU内存管理单元 https://www.cnblogs.com/alantu2018/p/9002309.html 之前对这一块一直不理解 最近学习了点 CPU time slice 以及 con ...
- 剑指offer58:对称的二叉树。判断一颗二叉树是不是对称的,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的
1 题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. 2 思路和方法 定义一种遍历算法,先遍历右子结点再遍历左子结点:如对称先序 ...