Pat1071: Speech Patterns
1071. Speech Patterns (25)
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 思路 找字符串中符合要求且出现次数最多的单词,用map模拟一个字典统计出现的单词就可以了。
注:注意最后一位如果是数字或者字母要再向字典插入一次。 代码
#include<iostream>
#include<map>
#include<ctype.h>
#include<iterator>
using namespace std;
int main()
{
string s;
map<string,int> dic;
getline(cin,s);
string tmp;
for(int i = ; i < s.size(); i++)
{
if(isalnum(s[i])) //isalnum检验是否是英文字符或者数字
{
tmp += tolower(s[i]);
}
if(!isalnum(s[i]) || i == s.size() - )
{
if(tmp.size() > )
dic[tmp]++;
tmp = "";
}
}
int curmax = ;
for(map<string,int>:: iterator it = dic.begin(); it != dic.end(); it++)
{
if(it->second > curmax)
{
tmp = it->first;
curmax = it->second;
}
}
cout << tmp << " " << curmax << endl;
}
Pat1071: Speech Patterns的更多相关文章
- PAT1071. Speech Patterns (25)
题目要求的Word定义 Here a "word" is defined as a continuous sequence of alphanumerical characters ...
- 【算法笔记】A1071 Speech Patterns
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- PAT 1071 Speech Patterns[一般]
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For exam ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- PAT_A1071#Speech Patterns
Source: PAT A1071 Speech Patterns (25 分) Description: People often have a preference among synonyms ...
- 1071 Speech Patterns——PAT甲级真题
1071 Speech Patterns People often have a preference among synonyms of the same word. For example, so ...
- 1071. Speech Patterns (25)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- Speech Patterns (string)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- A1071. Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
随机推荐
- FFMPEG列出DirectShow支持的设备
FFMPEG列出dshow支持的设备: ffmpeg -list_devices true -f dshow -idummy 举例: 采集摄像头和麦克风 ffmpeg -f dshow -i vide ...
- Palette状态栏颜色提取,写的不错就分享了
Palette 说Palette之前先说下前面提到的Pager.ViewPager是什么大家应该都是知道的了,一般ViewPager.xxxTabStrip.Fragment三个好基友是一起出现的.这 ...
- 一张图了解cocos2d坐标系
一张图了解cocos2d坐标系 平面直角坐标系
- sql的sum函数(与group by,having子句混合使用)
SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer='Bush' OR Customer='Adams' GROUP BY Custo ...
- iOS 10正式发布:十大新功能,更注重人性化
6月14日凌晨消息,苹果公司举行2016年WWDC全球开发者大会,介绍了watch OS.tv OS.OS X以及iOS 10系统的新特性. 据苹果介绍,iOS 10在锁屏.Siri.地图等十个各方面 ...
- 网站开发进阶(十一)如何将一个jsp页面嵌套在另一个页面中
如何将一个jsp页面嵌套在另一个页面中 这个在做网页中常要用到,有些通用的内容可集中放在一个页面文件中,其它要用到这些内容的页面只需要包含(引用)这个通用文件即可.这样便于维护,如果有很多网页,当通用 ...
- android 自定义gallerey并实现预览功能
自从Gallery被谷歌废弃以后,Google推荐使用ViewPager和HorizontalScrollView来实现Gallery的效果.的确HorizontalScrollView可以实现Gal ...
- iOS 百度地图计算两个点时间的距离
最近在解项目bug,更新地位城市的时候有个错误,后来想在位置改变多少距离之后,再取更新位置,这个功能去年做过.但是又忘记了! 所以还是记录一下吧. 百度地图提供了一个方法: BMKMapPointFo ...
- unix下的ACL
acl可以针对user,组,目录默认属性(mask)来控制. acl需要文件系统支持,ext2/3,jfs,xfs等都支持. getfacl setfacl 对于mac os X系统的acl 可以使用 ...
- 如何获取自己想要模拟的APP的相关图片?
一.首先打开iTunes APP,找到自己想要模拟实现的APP,并下载 二.找到下载的APP在iTunes中的位置 三.选中对应的APP,点击右键选择在Finder中显示,会打开对应的文件窗口,打开对 ...