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. JMeter:响应结果乱码解决方法

    JMeter:响应结果乱码解决方法 我们经常使用jmeter做接口测试或者正则匹配 看到的响应结果存在乱码,这是小白经常会问的问题,这是因为jmeter会按照jmeter.properties文件中, ...

  2. centos6下ActiveMQ+Zookeeper消息中间件集群部署记录

    由于最近一个项目并发请求压力比较大,所以考虑改进架构,引入消息中间件集群作为一个缓冲消息队列,具体需求:1)将大量的WebService请求报文发送到mq集群之中,并保持消息先后顺序2)保证每个消息的 ...

  3. Linux运维笔记-日常操作命令总结(3)

    文本操作:sed sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作. sed命令行格式为: sed [-nefri] ‘c ...

  4. part 1

    注意:本次源码分析选择2.0.3(因为不支持IE6.7.8,就少了很多兼容的hack的写法,对了解jQuery的实现原理有很大的帮助) 1.jQuery有不同的版本,从2.x版本便不再支持IE6.7. ...

  5. linux-流程控制语言

    if: for: 增强for循环 while: 统计这个目录下所有文件的大小 编写脚本 执行 help text:

  6. D. Cutting Out

    ---恢复内容开始--- 链接 [https://codeforces.com/contest/1077/problem/D] 题意 给你n,k,n个数,找出长度为k,的子串(不需连续),使得该子串数 ...

  7. 12.8 Daily Scrum

    最近大家都比较忙,任务今天也才刚刚分配,所以具体的编码任务从明天开始.   Tomorrow's Task 丁辛 完善餐厅列表,显示距离.             邓亚梅          美化搜索框 ...

  8. #个人博客作业Week1——浏览教材后提出的六个问题及软件与软件工程的提出。

    1.通常,我们阅读软件比编写软件花费的时间更多.正因为编写软件比阅读软件要容易,因此代码的可读性显得尤为重要.那么我们在写程序时应该如何避免多余的,带有误导性的注释,写出一个利于帮助别人读懂程序的注释 ...

  9. 剑指offer:字符串的排列

    题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: ...

  10. 第一次Spring总结

    第一阶段:下载了类似app使用,并做了对比,分析,对自己的app有了一些构思,完成了环境的配置.在这一阶段,一开始只有两个女生显得有点弱,面对从未接触过的app项目,首先就是配置环境方面的,在经过班上 ...