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. uinty实现玩家尾随鼠标位置平滑旋转角度

    首先我们要在场景中加入一个quad平面作为地板, 然后指定Layer为Floor,然后移除mesh renderer组件 然后加入脚本 脚本主要思想是从屏幕中心投出一条射线到地板, 然后获取相应坐标, ...

  2. dirname(__FILE__) 的使用总结 2(转)

    比如当前文件是放在(d:\www\)下,文件名是test.php. 测试的代码如下: 复制代码 代码如下: <?php echo __FILE__ ; // 取得当前文件的绝对地址,结果:D:\ ...

  3. nginx+keepalived构建高可用服务

    1.整体环境规划 虚拟IP:10.0.4.248 主Nginx:10.0.4.249 备用Nginx:10.0.4.250 2.keepalived安装 #cd /usr/local/src #wge ...

  4. jquery mobile demo

    <!DOCTYPE html> <html> <head> <title>jQuery Mobile Demo</title> <me ...

  5. Could not load type ‘System.ServiceModel.Activation.HttpModule’ from&

    1. 部署网站到IIS7.5,Window 2008的时候出现这个错误   2. 错误信息 Server Error in ‘/’ Application. Could not load type ‘ ...

  6. EF性能分析(一):动态SQL性能差.从OrderBy开始分析

    1. 问题背景 在我的力推下,部门业务开发转向ABP,其中ORM采用的是EntityFrameworkCore. 然而,在数据查询方面,出现了重大的性能问题... 请看代码: //在一个百万数据量的表 ...

  7. 红黑树red-black tree

    书籍:<算法导论>第13章 红黑树性质:1. 每个节点要么red要么black.2. 根节点是black节点.3. 叶子节点是black节点.4. red节点的左右儿子节点都是black节 ...

  8. 蓝桥杯 第三届C/C++预赛真题(5) 转方阵(C基本功)

    对一个方阵转置,就是把原来的行号变列号,原来的列号变行号 例如,如下的方阵: 1 2 3 4 5 6 7 8 9 10 11 1213 14 15 16 转置后变为: 1 5 9 13 2 6 10 ...

  9. hdu 3681(bfs+二分+状压dp判断)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...

  10. SurvivalShooter学习笔记(一.相机跟随)

    1.场景碰撞已好,地板需建一Quad去掉渲染留下碰撞,设置layer为Floor:用于建立摄像机朝向地面的射线,确定鼠标停留点,确定主角需要的朝向. 2.设置摄像机跟随主角: 本例中摄像机设置为正交模 ...