PAT 1071 Speech Patterns[一般]
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
题目大意:给出一个字符串,找出其中出现最多次数的word,这个word是由字母数字组成的。并且使大小写不敏感的。
下面代码在牛客网上AC,但是PAT上最后一个测试点没通过,答案错误。
#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
string lowCase(string s){
for(int i=;i<s.size();i++){
if(s[i]>='A'&&s[i]<='Z'){
s[i]=s[i]-'A'+'a';//如何将大写转换为小写呢?
}
}
return s;
}
int main() {
string s="";
map<string,int> mp;
char ch=getchar();
while(ch!='\n'){
if(isdigit(ch)||isalpha(ch)){
s+=ch;//加到字符串后。
}else {
if(s!=""){
mp[lowCase(s)]++;//放入map
// cout<<s<<" ";
s="";//被赋值为空。
}
}
ch=getchar();
}
int mx=;
for(auto it=mp.begin();it!=mp.end();it++){
if(it->second>mx){
mx=it->second;
s=it->first;
}
}
cout<<s<<" "<<mx;
return ;
}
果然发现了问题:

运行结果应该是3,而不是2。
#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
string lowCase(string s){
for(int i=;i<s.size();i++){
if(s[i]>='A'&&s[i]<='Z'){
s[i]=s[i]-'A'+'a';//如何将大写转换为小写呢?
}
}
return s;
}
int main() {
string s="";
map<string,int> mp;
char ch=getchar();
while(ch!='\n'){
if(isdigit(ch)||isalpha(ch)){
s+=ch;//加到字符串后。
//因为map不进去。
}else {
if(s!=""){
mp[lowCase(s)]++;//放入map
// cout<<s<<" "<<mp[s]<<'\n';
s="";//被赋值为空。
}
}
//cout<<ch;
ch=getchar();
if(ch=='\n'){//加上这个判断就AC了~~~
if(s!="")mp[lowCase(s)]++;//因为如果最后一个算不上。
}
}
int mx=;
//cout<<'\n';
for(auto it=mp.begin();it!=mp.end();it++){
if(it->second>mx){
mx=it->second;
s=it->first;
}
}
cout<<s<<" "<<mx;
return ;
}
// can can can
//学习了,这个字符串处理~~~
PAT 1071 Speech Patterns[一般]的更多相关文章
- PAT 1071. Speech Patterns
又是考输入输出 #include <cstdio> #include <cstdlib> #include <string> #include <vector ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- 1071 Speech Patterns——PAT甲级真题
1071 Speech Patterns People often have a preference among synonyms of the same word. For example, so ...
- PAT Advanced 1071 Speech Patterns (25 分)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- PAT (Advanced Level) 1071. Speech Patterns (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1071. Speech Patterns (25)-找出现最多的单词
分割字符串的用法+map映射给出input中出现次数最多的单词,如果次数相同,给出按字典序最小的. 这里我用了自定义分隔符来读取字符串,方法如下: //按照定义的分隔符d来分割字符串,对str进行读取 ...
- 【PAT甲级】1071 Speech Patterns (25 分)(getline(cin,x))
题意: 输入一行字符串,输出出现过次数最多的由字母和数字组成的字符串以及它出现的次数(对大小写不敏感,输出全部输出小写). AAAAAccepted code: #define HAVE_STRUCT ...
- 1071. Speech Patterns (25)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- 1071 Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
随机推荐
- R语言安装openxl包报错解决办法
在R语言中使用openxlsx包,会报错 解决办法就是: 下载安装Set-Rtool,安装时注意勾选对话框 然后在R中运行以下代码: Sys.setenv("R_ZIPCMD" = ...
- HBase之HFile解析
Sumary: Protobuf BinarySearch 本篇主要讲HFileV2的相关内容,包括HFile的构成.解析及怎么样从HFile中快速找到相关的KeyValue.基于Hbase 0.98 ...
- Jmeter负载测试例子
通过浏览器操作网站在jmeter录屏控制器显示录屏例子,并且通过这例子模拟多用户(线程)来负载测试. 工具/原料 Jmeter 浏览器 1.先在测试计划创建线程组和录制Case 1 1.1 选 ...
- Oracle安装完成后,如何用命令行启动和关闭数据库?
Oracle安装完成后,如何用命令行启动和关闭数据库? 解答: 打开:STARTUP [FORCE] [RESTRICT] [PFILE= filename] [OPEN [RECOVER][ dat ...
- HP proliant服务器从usb启动
1,开机出现自检画面开始按F9进入设置,进入BIOS 选择standard boot order(rpl),把usb driver放在第一位,保存好 2,按F1开始启动. (注:我使用ubuntu14 ...
- 对Linux命令进一步学习
root@wuheng-virtual-machine:/home/wuheng# ls -ltotal 44drwxr-xr-x 2 wuheng wuheng 4096 Mar 3 01:30 ...
- 计算机视觉中的边缘检测Edge Detection in Computer Vision
计算机视觉中的边缘检测 边缘检测是计算机视觉中最重要的概念之一.这是一个很直观的概念,在一个图像上运行图像检测应该只输出边缘,与素描比较相似.我的目标不仅是清晰地解释边缘检测是怎样工作的,同时也提 ...
- string与wstring互转
string与wstring互转 C++ Code 123456789101112131415161718192021222324252627282930313233343536373839404 ...
- springside4
https://github.com/springside/springside4/wiki/Design Design 1. Web MVC Framwork: SpringMVC3.0 Restf ...
- python中的 try...except...finally 的用法
python中的 try...except...finally 的用法 author:headsen chen date:2018-04-09 16:22:11 try, except, final ...