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<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<string>
using namespace std;
map <string, int>mp;
char str[], word[];
vector<string> maxWord;
bool cmp(string a, string b){
return a < b; }
int main(){
int i,j, index, maxN = -;
string ss;
char c;
gets(str);
int find;
for(i = ; str[i] != '\0'; i++){
index = ;
while(str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' || str[i] >= '' && str[i] <= ''){
c = str[i++];
if(c >= 'A' && c <= 'Z')
c = c - 'A' + 'a';
word[index++] = c;
}
word[index] = '\0';
if(index != ){
ss = word;
int cnt;
if(mp.count(ss) == ){
mp[ss] = ;
cnt = ;
}else{
mp[ss] = mp[ss] + ;
cnt = mp[ss];
}
if(cnt > maxN)
maxN = cnt;
}
}
for(map<string, int>::iterator it = mp.begin(); it != mp.end(); it++){
if(it->second == maxN)
maxWord.push_back(it->first);
}
sort(maxWord.begin(), maxWord.end(), cmp);
for(int i = ; i < maxWord.size(); i++){
cout << maxWord[i] << " ";
}
printf("%d", maxN);
cin >> i;
return ;
}

总结:

1、组装单词,将其用map<string, int> 记录出现次数, 并且记录最大值。当扫描完后,遍历map,找出所有出现次数等于最大值的单词,将其按字典排序输出。

2、alphanumerical :字母和数字都算单词。

A1071. Speech Patterns的更多相关文章

  1. 【算法笔记】A1071 Speech Patterns

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

  2. PAT甲级——A1071 Speech Patterns

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

  3. A1071 Speech Patterns (25 分)

    一.技术总结 开始拿到这道题目时,思考的是我该如何区分它们每一个单词,不知道这里还是要学习得知在cctype头文件中有一个函数用于查看是否为0~9.a~z.A~Z,就是isalnum(),又因为题目中 ...

  4. PAT_A1071#Speech Patterns

    Source: PAT A1071 Speech Patterns (25 分) Description: People often have a preference among synonyms ...

  5. Pat1071: Speech Patterns

    1071. Speech Patterns (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming Peo ...

  6. PAT 1071 Speech Patterns[一般]

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

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

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

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

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

  9. 1071. Speech Patterns (25)

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

随机推荐

  1. Mybatis 中 columnPrefix别名的用法

    1.映射对应的属性,区分他们分别属于哪些类.(sql书写的时候为什么要将前缀加上(别名),是因为便于它去寻找哪个类的前缀是ANNEX_) 2.例:  如下所示当一个collection 定义了一个co ...

  2. 使用阿里云cli管理安全组

    相比于python SDK方式,阿里云基于GO SDK开发了一整套CLI工具,可以通过调用RPC API来管理云资源,对编程能力不够的人来说是个福音. 而且,阿里云CLI的文档比SDK的文档更加全面, ...

  3. html绝对路径,相对路径

    .com/eat.php中引用.com/includes/headrt.php的话写includes/header.php .com/service/eat.php中引用.com/includes/h ...

  4. Ajax的注意事项

    case 1: 无论是使用原生的JavaScript,还是JQuery,通过Ajax请求后端程序数据,返回的数据默认是字符串,字符串,字符串,重要的事情说三遍!!! case 2: 不要尝试直接将返回 ...

  5. mongoDB的配置和使用

    如何启动mongodb? mongod --dbpath C:\appStore\mongodata //数据库地址 再开一个cmder窗口 进入C:\Program Files\MongoDB\Se ...

  6. 数组操作方法(包括es5)

    //push(); 定义:可以可向数组的末尾添加一个或更多元素,并返回新的长度. 方法:push(); 语法:数组.push(新元素1,新元素2,....,新元素x) 返回值:把指定的值添加到数组后的 ...

  7. 如何批量删除Docker中已经停止的容器

    如何批量删除Docker中已经停止的容器   方法一: #显示所有的容器,过滤出Exited状态的容器,取出这些容器的ID, sudo docker ps -a|grep Exited|awk '{p ...

  8. [读书笔记]SQLSERVER企业级平台管理实践读书笔记01

    1. SQLSERVER信息收集 SQLDIAG 使用界面 C:\Users\Administrator>sqldiag2018/01/02 08:13:26.10 SQLDIAG Collec ...

  9. mysql 数据表备份导出,恢复导入操作实践

    因为经常跑脚本的关系, 每次跑完数据之后,相关的测试服数据库表的数据都被跑乱了,重新跑脚本恢复回来速度也不快,所以尝试在跑脚本之前直接备份该表,然后跑完数据之后恢复的方式,应该会方便一点.所以实践一波 ...

  10. Delphi2007精简版加载Borland.Studio.Together.dll错误解决办法

    安装Delphi2007精简版,启动提示Borland.Studio.Together.dll加载错误,错误信息如下: Failed to load IDE add in 'C:\Program Fi ...