PAT 甲级 1071 Speech Patterns (25 分)(map)
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)的更多相关文章
- PAT Advanced 1071 Speech Patterns (25 分)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- 【PAT甲级】1071 Speech Patterns (25 分)(getline(cin,x))
题意: 输入一行字符串,输出出现过次数最多的由字母和数字组成的字符串以及它出现的次数(对大小写不敏感,输出全部输出小写). AAAAAccepted code: #define HAVE_STRUCT ...
- PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- 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 ...
- 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 ...
- 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 ...
- PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an ...
随机推荐
- BeautifulSoup模板简单应用-提取html指定数据(api_name/api_method/api_path,请求body/请求header/pagam参数)
from bs4 import BeautifulSoup import re import os.path import itertools name='newcrm' source_file_pa ...
- PAT1059Prime Factors
1059 Prime Factors (25分) Given any positive integer N, you are supposed to find all of its prime f ...
- js数值的添加与删除
js中数组元素的添加和删除 js中数组元素常用添加方法是直接添加.push方法以及unshift方法 删除方法则是delete.pop.shift 集修改方法为一身的则是splice 1.添加: (1 ...
- noi.ac #46 最长上升子序列
\(des\) 长度为 \(n\) 的序列 \(A\),从中删去恰好 \(k\) 个元素(右边的元素往左边移动),记 \(cnt\) 为新 序列中 \(Ai = i\) 的元素个数(即权值与下标相同的 ...
- Unity 绘制Mesh线条
using UnityEngine; using System.Collections; using System.Collections.Generic; public struct Segme ...
- Three.js中的group,obj,模型中layers层级与相机
目录 Three.js中的group,obj,模型中layers层级与相机 layers group中的.layers.mask设置 一些设想 Three.js中的group,obj,模型中layer ...
- NAT双线路配置详解
- java读取文件内容并输出到控制台,java中实现文件复制
public class TestFileInputStream { public static void main(String [] args) { //读取指定文件中内容,并在控制台输出 Fil ...
- 数据结构Java版之堆&堆排序(九)
堆分为大顶堆,和小顶堆. 什么是堆? 堆可以看成是一棵二叉树,二叉树的元素是一个数组不断的从左到右轮训放置.如果是大顶堆,则大的数放上面一层,小的数放下面一层.上一层的数,一定大于下一层的数.小顶堆则 ...
- 使用IDEA运行CAS5.3服务器
在上节中,我们运行CAS服务器是打成war包在tomcat中进行运行,这节介绍在IDEA中运行CAS服务器. 1.下载CAS 模板 Overlay Template,我这里使用 Apereo CAS ...