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

题意

  统计一句话中出现次数最多的单词,输出这个单词和出现次数。注意can1也算一个单词。

思路

  步骤1:把每个单词从句子里分离出来。

  步骤2:用map记录出现次数。

  步骤3:遍历map,输出次数最多的单词。

code:

 #include<bits/stdc++.h>
using namespace std;
map<string, int> wordStat;
bool check(char c){
if(c>=''&&c<='') return true;
if(c>='A'&&c<='Z') return true;
if(c>='a'&&c<='z') return true;
return false;
}
int main(){
string str, pattern;
int i = , max = ;
getline(cin, str);
while(i < str.length()){
string word;
while(i < str.length() && check(str[i])==true){
if(str[i]>='A'&&str[i]<='Z') str[i] += ;
word += str[i];
i++;
}
if(word != ""){
if(wordStat.find(word) == wordStat.end()) wordStat[word] = ;
else wordStat[word]++;
}
while(i < str.length() && check(str[i])==false){
i++;
}
}
for(map<string, int>::iterator it = wordStat.begin(); it!=wordStat.end(); it++){
if(max <= it->second){
max = it->second;
pattern = it->first;
}
}
cout<<pattern<<" "<<max;
return ;
}

【算法笔记】A1071 Speech Patterns的更多相关文章

  1. A1071. Speech Patterns

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

  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. 学习Java 以及对几大基本排序算法(对算法笔记书的研究)的一些学习总结(Java对算法的实现持续更新中)

    Java排序一,冒泡排序! 刚刚开始学习Java,但是比较有兴趣研究算法.最近看了一本算法笔记,刚开始只是打算随便看看,但是发现这本书非常不错,尤其是对排序算法,以及哈希函数的一些解释,让我非常的感兴 ...

  6. Pat1071: Speech Patterns

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

  7. 算法笔记--数位dp

    算法笔记 这个博客写的不错:http://blog.csdn.net/wust_zzwh/article/details/52100392 数位dp的精髓是不同情况下sta变量的设置. 模板: ]; ...

  8. 算法笔记--lca倍增算法

    算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...

  9. 算法笔记--STL中的各种遍历及查找(待增)

    算法笔记 map: map<string,int> m; map<string,int>::iterator it;//auto it it = m.begin(); whil ...

随机推荐

  1. c++ template 判断是否为类类型

    /* The following code example is taken from the book * "C++ Templates - The Complete Guide" ...

  2. CodeForces 518A Vitaly and Strings (水题,字符串)

    题意:给定两个相同长度的字符串,让你找出一个字符串,字典序在两都之间. 析:这个题当时WA了好多次,后来才发现是这么水,我们只要把 s 串加上,然后和算数一样,该进位进位,然后再和 t 比较就行. 代 ...

  3. ZOJ2748 Free Kick 2017-04-18 20:40 40人阅读 评论(0) 收藏

    Free Kick Time Limit: 2 Seconds      Memory Limit: 65536 KB In a soccer game, a direct free kick is ...

  4. 23 DesignPatterns学习笔记:C++语言实现 --- 2.1 Bridge

    23 DesignPatterns学习笔记:C++语言实现 --- 2.1 Bridge 2016-07-22 (www.cnblogs.com/icmzn) 模式理解  

  5. [javascript]两段 javaScript 代码的逻辑比较

    两段 javaScript 代码的逻辑比较: #1 if(tagName.length < 3){    $(this).parent().addClass('active');    tagN ...

  6. PostgreSQL 表空间

    PostgreSQL 表空间 一 介绍使用表空间可以将不同的表放到不同的存储介质或不同的文件系统下,实际上是为表指定一个存储的目录.创建数据库,表,索引时可以指定表空间,将数据库,表,索引放到指定的目 ...

  7. 命令式语言和声明式语言对比——JavaScript实现快速排序为例

    什么是命令式编程 (Imperative Programming)? 命令机器如何做事情,强调细节实现 java.c.c++等都属此类. “这些语言的特征在于,写出的代码除了表现出“什么(What)” ...

  8. 微服务编译、启动jar命令指定配置文件

    nohup java -Xms512m -Xmx8g -Xmn512m -Xss512k -server -XX:+HeapDumpOnOutOfMemoryError -jar smp-bill-c ...

  9. 将引用了第三方jar包的Java项目打包成jar文件的两种方法

    方案一:用Eclipse自带的Export功能 步骤1:准备主清单文件 “MANIFEST.MF”, 由于是打包引用了第三方jar包的Java项目,故需要自定义配置文件MANIFEST.MF,在该项目 ...

  10. c# 前后日期设置

    List<string> list = new List<string>(); //根据当月 显示前6个月 for(int i=0;i<6;i++) { list.add ...