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 ...
随机推荐
- Java进阶(二十)解疑答惑之何时字符串才算真正为空?
解疑答惑之何时字符串才算真正为空? 在一次编码过程中,有一个现象一直困扰着自己,经过后台的不断调试,才发现原来有时候字符串的空非空.测试代码如下: // medname可为药品名称或药品ID Stri ...
- 循环链表设计与API实现
基本概念 循环链表的定义:将单链表中最后一个数据元素的next指针指向第一个元素 循环链表拥有单链表的所有操作 创建链表 销毁链表 获取链表长度 清空链表 获取第pos个元素操作 插入元素到位置pos ...
- Makefile的obj-y 和 obj-m
目标定义是Kbuild Makefile的主要部分,也是核心部分.主要是定义了要编 译的文件,所有的选项,以及到哪些子目录去执行递归操作. 最简单的Kbuild makefile 只包含一行: 例子: ...
- 插件化开发—动态加载技术加载已安装和未安装的apk
首先引入一个概念,动态加载技术是什么?为什么要引入动态加载?它有什么好处呢?首先要明白这几个问题,我们先从 应用程序入手,大家都知道在Android App中,一个应用程序dex文件的方法数最大不能超 ...
- 设置布局默认为LinearLayout,却成了RelativeLayout
GoogleXML布局文件前推荐布局LinearLayout新建布局XML文件根元素LinearLayout, 随着android发展工程师更推荐使用RelativeLayout布局式所新建XML布局 ...
- FNDCPASS Troubleshooting Guide For Login and Changing Applications Passwords
In this Document Goal Solution 1. Error Starting Application Services After Changing APPS Pass ...
- LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II
1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...
- Oracel 编写控制结构
1.条件分支语句 在Oracle9i之前,执行条件分支操作都需要使用IF语句来完成,并且PL/SQL中,提供了三种条件分支语句:IF-THEN.IF-THEN-ELSE.IF-THEN-ELSIF.具 ...
- Course2-Python函数和模块
一. 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率. 上一课里提到了Python的很多内置函数.在此主要讲自定义函数. 1. 定 ...
- 数据库面试题目- ORACLE
Posted on 2009-06-08 17:38 漠北的天空 阅读(110) 评论(0) 编辑 收藏 1. 列举几种表连接方式 Answer:等连接(内连接).非等连接.自连接.外连 ...