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
#include<iostream>
#include<map>
#include<string>
using namespace std;
bool check(char c){
if(c >= '' && c <= '') return true;
if(c >= 'a' && c <= 'z') return true;
if(c >= 'A' && c <= 'Z') return true;
return false;
}
int main(){
map<string,int> count;
string str;
getline(cin,str);
int i = ;
while(i < str.length()){
string word;
while(i < str.length() && check(str[i]) == true){
if(str[i] >= 'A' && str[i] <='Z'){
str[i] += ;
}
word += str[i];
i++;
}
if(word != " "){
if(count.find(word)!=count.end()) count[word]++;
else count[word] = ;
}
while(i < str.length() && check(str[i]) == false){
i++;
}
}
string ans;
int max = ;
for(map<string,int>::iterator it = count.begin(); it!=count.end();it++){
if(it -> second > max){
ans = it -> first;
max = it -> second;
}
}
cout << ans << " " << max << endl;
return ;
}

20200104

最后一个测试点没通过,应该是空符的问题,待排查

#include<iostream>
#include<string>
#include<map>
using namespace std; bool check(char c); int main()
{
string str;
string word;
map<string, int> mp; getline(cin, str); int len = str.length(); for (int i = ; i < len; i++)
{
if (check(str[i]))
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] += ;
}
word += str[i];
continue;
}
else if (word != "" && (!word.empty()))
{
if (mp.find(word) != mp.end())
{
mp[word]++;
}
else
{
mp[word] = ;
}
word.clear();
}
} string ans;
int max = ;
for (auto it = mp.begin(); it != mp.end(); it++)
{
if (it->second > max && it->first != "")
{
ans = it->first;
max = it->second;
}
} cout << ans << " " << max << endl;
return ;
} bool check(char c)
{
if (c >= '' && c <= '')
{
return true;
}
else if (c >= 'a' && c <= 'z')
{
return true;
}
else if (c >= 'A' && c <= 'Z')
{
return true;
} return false;
}

1071 Speech Patterns (25)(25 分)的更多相关文章

  1. PAT 1071 Speech Patterns[一般]

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

  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甲级】1071 Speech Patterns (25 分)(getline(cin,x))

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

  6. 1071. Speech Patterns (25)

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

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

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

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

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

  9. 1071 Speech Patterns

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

随机推荐

  1. resnet的理解-- 面试笔记

    上周参加了XX大学研究生推免的面试,面试老爷问到了resnet主要解决了什么问题,我下意识的回答到解决了当网络加深的时候会出现的vanishing/exploding gradients,然后面试老爷 ...

  2. java之单元测试

    这篇主要简单讲下java的单元测试 目录结构如下: 如图,其中1是需要被测试的功能:2是测试模块:3是单元测试需要的引入包: 1. 功能模块1中 Calculator 的代码: package cn. ...

  3. java报错与解决方法总结

    错误 error:Syntax error, insert ")" to complete MethodDeclaration 解决办法:放到main方法里 错误原因: 错误: e ...

  4. jvm调优相关

    查找占用cpu过高的线程,并排查原因 1.查找jvm进程 (1)jps -l (2)ps -ef|grep java 这一步骤可以得到进程号,假如进程号为9527 2.查找该jvm进程中占用cpu比较 ...

  5. Git创建工作目录与常用指令

    1.创建工作目录与常用指令 工作目录(WorkSpace)一般就是你希望Git帮助你管理的文件夹,可以是你项目的目录,也可以是一个空目录,建议不要有中文. 日常使用只要记住下图6个命令: 2.提交管理 ...

  6. Windows环境下大数据处理的构想(一)

    为什么不呢?我们有了RPC/RMI和MAP,为什么不能在windows环境下处理大数据呢?windows是迄今为止最普及的操作系统,据市调公司NetMarketShare最新(2019年5月)统计数据 ...

  7. C# is与 java instanceof 作用相同

    c#中 is 检查对象是否与给定类型兼容. 例如,下面的代码可以确定对象是否为 MyObject 类型的一个实例,或者对象是否为从 MyObject 派生的一个类型: if (obj is MyObj ...

  8. Linux命令——getconf

    转自:灵活使用getconf命令来获取系统信息 简介 getconf本身是个ELF可执行文件,用于获取系统信息 用法 getconf -a可以获取全部系统信息 对于这个命令,记住几个常用的信息获取方法 ...

  9. linux 进程管理与调度(一)

    进程结构 进程在内核的源代码中以结构体表示,篇幅很长,在此列举一小段关键代码,可以发现是个双向链表,具体的可以在内核目录下找一个叫"sched.h"的头文件. struct tas ...

  10. sklearn 翻译笔记:KNeighborsClassifier

    今天做机器学习knn的实现想使用sklearn这个模块,但是里面的函数不懂,无奈只能查文档,但是一大片英文看见我就烦,也不是说不能看  但是以我低下的英语水平实在是太费劲了.幸好找到一篇前人翻译的比较 ...