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. love2d教程34--thread模块

    love的thread是一个单独的lua运行环境,与主线程平行.因此可以用线程来处理 处理复杂的计算,不过由于隔离,线程不能访问主线程的变量和方法,而且进程 间通信也受限.   可以在线程里共享lov ...

  2. 标准差分进化算法matlab程序实现(转载)

    标准差分进化算法matlab程序实现 自适应差分演化算法方面的Matlab和C++代码及论文 差分进化算法 DE-Differential Evolution matlab练习程序(差异演化DE) [ ...

  3. pl/sql 实例精解 06

    1. 简单循环 1: LOOP 2: statement1; 3: statement2; 4: EXIT WHEN condition; 5: END LOOP; 6: statement3; 也可 ...

  4. Django升级到1.10,MEDIA_ROOT发生TypeError错误

    Getting error: “TypeError: view must be a callable or a list/tuple in the case of include().” when r ...

  5. Linux文件的打包与压缩

    打包命令: tar tar 的选项与参数非常的多!我们只讲几个常用的选项,更多选项您可以自行 man tar 查询罗! [root@www ~]# tar [-j|-z] [cv] [-f 创建的档名 ...

  6. Objective-C中的instancetype和id关键字(转)

    转自:Objective-C中的instancetype和id关键字 一.什么是instancetype 同id一样,都是表示未知类型的的对象. 二.关联返回类型(related result typ ...

  7. 广义高斯分布(GGD)

    广义高斯分布(GGD)-Generalized Gaussian Distribution 广义高斯分布及其在图像去噪的应用_百度文库 https://wenku.baidu.com/view/2b8 ...

  8. commit和rollback

    COMMIT过程·已经在 SGA(系统全局区域)中产生了回滚段(Rollback segment)记录.·已经在 SGA 中产生了修改数据块.·已经在 SGA 中产生了上面两条的缓冲重做(redo). ...

  9. 一起学android之设置ListView数据显示的动画效果(24)

    效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/40 ...

  10. redis问题集

    Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有序集合SortedSet. 如果你是Redis中高级用户,还需要加上下面几种数据结构HyperLogLog.G ...