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. Maven过滤属性文件,替换属性值

    pom.xml 1.resources: resources中是定义哪些目录下的文件会被配置文件中定义的变量替换,一般我们会把项目的配置文件放在src/main/resources下,像db,bean ...

  2. Spring Cloud Alibaba学习笔记(20) - Spring Cloud Gateway 内置的全局过滤器

    参考:https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_global_filter ...

  3. C#解压、压缩高级用法

    压缩:(可以吧要排除的文件去掉) /// <summary> /// 压缩文件夹 /// </summary> /// <param name="folder& ...

  4. springboot打成jar包后无法解压

    springboot打成jar包后无法解压 Springboot打出来的jar,用压缩工具解压报错.Why? 先说解决办法. 1.解决办法 executable属性导致的,属性改成false后重新打包 ...

  5. java之servlet之文件下载

    1.在页面中,可以直接通过超链接来下载: a) 如果浏览器能够打开该文件,那么直接在浏览器中显示---不是想要的效果 b) 任何人都能下载,不能进行权限控制 2.通过servlet来进行下载,在ser ...

  6. js json数据保存到本地

    转自:https://www.cnblogs.com/gamedaybyday/p/9906542.html 使用HTML5来实现本地文件读取和写入  (FileReader读取json文件,File ...

  7. Java调用WebService方法总结(6)--XFire调用WebService

    XFire是codeHaus组织提供的一个WebService开源框架,目前已被Apache的CXF所取代,已很少有人用了,这里简单记录下其调用WebService使用方法.官网现已不提供下载,可以到 ...

  8. 2019年北航OO第4单元(UML)总结

    1 架构设计 经过了接近一学期的程序设计训练,在这一单元的第一次作业中我就非常注重架构的设计,竭力避免像之前一样陷入"第一次作业凑合,第二次作业重构"的不健康的迭代模式.整体上来说 ...

  9. python Linux 环境 (版本隔离工具)

    python Linux 环境 (版本隔离工具) 首先新建用户,养成良好习惯useradd python 1.安装pyenv GitHub官网:https://github.com/pyenv/pye ...

  10. mybatis的XML中注释需谨慎

    报错内容: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2) ...