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

题意:

给定一个字符串,从中找出重复次数最多的符合要求的单词,输出该单词以及出现次数。组成该单词的字符只能出自[0-9 A-Z  a-z]集合。如果有出现次数相同的单词,则按照单词的字典序,输出第一个单词。

题解:

我还以为有个人名,结果不用管开头的人名。

使用map很方便

AC代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
using namespace std;
map<string,int>m;
int main(){
string name,s;
getline(cin,s);
int l=s.length();
string word="";
for(int i=;i<l;i++){
s[i]=tolower(s[i]);
if(isalnum(s[i])) word+=s[i];
else{
if(word!="") {
if(m.find(word)==m.end()) m[word]=;
else m[word]++;
}
word="";
}
}
if(word!="") m[word]++;
string ans;
int count=;
map<string,int>::iterator it;
for(it=m.begin();it!=m.end();it++){
if(it->second>count){
count=it->second;
ans=it->first;
}
}
cout<<ans<<" "<<count;
return ;
}

PAT 甲级 1071 Speech Patterns (25 分)(map)的更多相关文章

  1. PAT Advanced 1071 Speech Patterns (25 分)

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  2. 【PAT甲级】1071 Speech Patterns (25 分)(getline(cin,x))

    题意: 输入一行字符串,输出出现过次数最多的由字母和数字组成的字符串以及它出现的次数(对大小写不敏感,输出全部输出小写). AAAAAccepted code: #define HAVE_STRUCT ...

  3. PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)

    1048 Find Coins (25 分)   Eva loves to collect coins from all over the universe, including some other ...

  4. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  5. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  6. PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

    1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be ...

  7. PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)

    1059 Prime Factors (25 分)   Given any positive integer N, you are supposed to find all of its prime ...

  8. PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

    1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ord ...

  9. PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)

    1037 Magic Coupon (25 分)   The magic shop in Mars is offering some magic coupons. Each coupon has an ...

随机推荐

  1. drf框架 - JWT认证插件

    JWT认证 JWT认证方式与其他认证方式对比: 优点 1) 服务器不要存储token,token交给每一个客户端自己存储,服务器压力小 2)服务器存储的是 签发和校验token 两段算法,签发认证的效 ...

  2. Linux系统 安装JDK和tomcat

    下载文件路径包: http://archive.apache.org/dist/ 首先将软件包上传到/tmp目录下 需要文件如下 jdk-8u60-linux-x64.gz apache-tomcat ...

  3. oracle数据库创建表且主键自增

    唠叨几句:几年前的知识忘却了,整理一下笔记,提供一下方便 1.创建数据库表 设置主键 create table users( userid number(10) primary key, /*主键,自 ...

  4. MongoDB 主从复制及 自动故障转移

    1.MongoDB 主从复制 MongoDB复制是将数据同步在多个服务器的过程. 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性. 复制还允许您从 ...

  5. java 如何遍历Map对象

    内容介绍 在java中遍历Map对象的方法. Map对象 Map<String,Object> map = new HashMap<>(); map.put("xia ...

  6. es 启动问题

    max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536] vim / ...

  7. learning java AWT 布局管理器BorderLayout

    BorderLayout 将容器分为EAST, SOUTH, WEST,NORTH,CENTER五个区域. public class BorderLayoutTest { public static ...

  8. noi.ac #36 模拟

    \(des\) 存在 \(1000 \times 1000\) 的矩阵,保证元素互不相同,\(2e5\) 次询问,每次询 问给定 \(x, y\) 问存在多少点 \((a, b)\) 满足该元素是 \ ...

  9. 【一起来烧脑】读懂Promise知识体系

    知识体系 Promise基础语法,如何处理错误,简单介绍异步函数 内容 错误处理的两种方式: reject('错误信息').then(null, message => {}) throw new ...

  10. MYSQL安装报错需要.NET4.0

    MySQL安装,提示需要.NET4.0 解决:下载安装.NET4.0即可 链接:https://pan.baidu.com/s/1u0e0lafBbR0QYEcXFsv9sQ 提取码:q6rr 复制这 ...