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[一般]的更多相关文章

  1. PAT 1071. Speech Patterns

    又是考输入输出 #include <cstdio> #include <cstdlib> #include <string> #include <vector ...

  2. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

  3. 1071 Speech Patterns——PAT甲级真题

    1071 Speech Patterns People often have a preference among synonyms of the same word. For example, so ...

  4. PAT Advanced 1071 Speech Patterns (25 分)

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

  5. PAT (Advanced Level) 1071. Speech Patterns (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  6. PAT甲题题解-1071. Speech Patterns (25)-找出现最多的单词

    分割字符串的用法+map映射给出input中出现次数最多的单词,如果次数相同,给出按字典序最小的. 这里我用了自定义分隔符来读取字符串,方法如下: //按照定义的分隔符d来分割字符串,对str进行读取 ...

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

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

  8. 1071. Speech Patterns (25)

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

  9. 1071 Speech Patterns

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

随机推荐

  1. spring的数据源

    Spring提供了两个这样的数据源(都位于org.springframework.jdbc.datasource程序包里):        DriverManagerDataSource:在每个连接请 ...

  2. STM32F10x_硬件I2C读写EEPROM(标准外设库版本)

    Ⅰ.写在前面 上一篇文章是“STM32F10x_模拟I2C读写EEPROM”,讲述使用IO口模拟I2C总线通信,对EEPROM(AT24Xxx)进行读写操作的过程. 上一篇文章主要内容:I2C协议.模 ...

  3. 通用采集器Modbus协议应用

    1.  功能码 通用采集器一般包含DI,DO,AI相关接口,对此类接口主要应用功能码01~06. 3类接口具体对应关系如下: 继电器定义,功能码01/05(01:读线圈,05写线圈) 序号       ...

  4. Linux上安装Nginx及常用命令

    一.Linux安装软件常用方法 1.rpm(或pkg)安装,类似于Windows安装程序,是预编译好的程序. 1)使用的是通用参数编译,配置参数不是最佳 2)可控制性不强,比如对程序特定组件的定制性安 ...

  5. spring security 一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架

    Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中 配置的Bean,充分利用了Spring ...

  6. Pyspider学习

    简介 国人编写的强大的网络爬虫系统并自带强大的WebUI,采用Python语言编写吗,分布式架构.支持多种数据库后端 pyspider中文网站:http://www.pyspider.cn/ 源码网址 ...

  7. UVA1349 Optimal Bus Route Design 拆点法+最小费用最佳匹配

    /** 题目:UVA1349 Optimal Bus Route Design 链接:https://vjudge.net/problem/UVA-1349 题意:lrj入门经典P375 给n个点(n ...

  8. [转]Tomcat和Apache的区别和联系

    Apache,指的是Apache软件基金会下的一个项目——Apache HTTP Server Project:Nginx同样也是一款开源的HTTP服务器软件(当然它也可以作为邮件代理服务器.通用的T ...

  9. 为什么 MongoDB 连接数被用满了?

    使用 MongoDB 时,可能会遇到因为 mongod 连接数用满了,导致客户端无法连接的问题.mongod的最大连接数通过 net.maxIncomingConnections 指定,默认值为100 ...

  10. 说明Oracle数据库逻辑备份和物理备份的方式。

    说明Oracle数据库逻辑备份和物理备份的方式. 解答:Oracle备份包括逻辑备份和物理备份. 1).逻辑备份 数据库的逻辑备份包含读一个数据库记录集和将记录集写入文件. a.输出(Export)输 ...