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. 程序员眼中的Redis

    Redis 是用C语言编写的内存中的数据结构存储系统,可以用来作数据库.缓存.消息中间件. 数据结构 字符串(strings):值是任何种类的字符串 散列(hashs):值是map 字典,数组+链表, ...

  2. 分布式监控系统Zabbix3.4-针对MongoDB性能监控操作笔记

    公司在IDC机房的一台服务器上部署了MongoDB,由于所存储的业务数据比较重要,所以对MongoDB的监控显得尤为重要!Zabbix监控MongoDB性能的原理:通过echo "db.se ...

  3. Nginx 负载均衡的Cache缓存批量清理的操作记录

    1)nginx.conf配置 [root@inner-lb01 ~]# cat /data/nginx/conf/nginx.conf user www; worker_processes 8; #e ...

  4. db2修改最大连接数

    查看当前连接数,sample为数据库名db2 list applications for db sample db2 list applications for db sample show deta ...

  5. Spring RPC 入门学习(3)-插入Student对象

    Spring RPC 向后台传递对象 1. 新建RPC接口:StudentInterface.java package com.cvicse.ump.rpc.interfaceDefine; impo ...

  6. 求去掉一条边使最小割变小 HAOI2017 新型城市化

    先求最小割,然后对残量网络跑Tarjan.对于所有满流的边,若其两端点不在同一个SCC中,则这条边是满足条件的. 证明见 来源:HAOI2017 新型城市化

  7. 作业20171026 alpha-2及alpha发布成绩

    申诉 对成绩有疑问或不同意见的同学,请在群里[@杨贵福]. 申诉时间截止2017年11月21日 17:00. 成绩 scrum01 scrum02 scrum03 scrum04 scrum05 sc ...

  8. Linux内核分析 读书笔记 (第四章)

    第四章 进程调度 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间.进程调度程序可看做在可运行态进程之间分配有限的处理器时间资源的内核子系统.只有通过调度程序的合理调度,系统资源才能最大限 ...

  9. 实践——ELF文件格式分析

    一.分析文件头 1. 段入口类型定义(/usr/include/elf.h)下面产生的hello是32位的 使用命令#Hexdump –x ELF_1.o 第一行: 前4字节,蓝色部分,是一个魔数,表 ...

  10. Oracle系列(三): 情景查询一 a表中有个fid字段,逗号分隔开来,b表中有id字段及其他信息,如何关联a表的fid和和b表的id字段查询

    现在有两个表,表a中 DOC FID 1 a,b,c 2 a,c,d 表b中 ID KEY a A b B c C d D 怎么联合查询出 DOC FID KEY 1 a,b,c A,B,C 2 a, ...