浙江大学PAT_甲级_1071. Speech Patterns (25)
来源:互联网 发布:aspen8.4软件下载 编辑:程序博客网 时间:2024/06/01 09:53
题目链接:点击打开链接
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我的C++代码:
#include<iostream>#include<map>#include<string>#include<vector>#include<algorithm>using namespace std;int main(){string sentence;getline(cin, sentence);//输入一句话map<string, int> word_frequency;//单个单词出现的次数string word;//单词int i = 0;vector<string>vec_word;//存储单词的vectorint max_frequency = 0;//最大出现次数string most_commonly_word;//出现次数最多的单词for (i = 0; i <sentence.length(); i++)//把一句话分解成单词组成的vector{word= "";while (isalnum(sentence[i]))//字符是数字或字母{if (isupper(sentence[i]))//大写转小写sentence[i] = tolower(sentence[i]);word += sentence[i];//得到单个的单词i++;}if (word!= "")vec_word.push_back(word);//把单词放进vector}for (int i = 0; i<vec_word.size(); i++)//把单词放进map{word_frequency [ vec_word[i] ]++;//记录每个单词的出现次数if (max_frequency <word_frequency [vec_word[i]] )//查找出现次数最多的单词{max_frequency = word_frequency [vec_word[i]];most_commonly_word = vec_word [i];}}cout << most_commonly_word << " " << max_frequency;//输出最常见的单词,和出现次数//system("pause");return 0;}
- 浙江大学PAT_甲级_1071. Speech Patterns (25)
- 浙江大学PAT_甲级_1024. Palindromic Number (25)
- 浙江大学PAT_甲级_1028. List Sorting (25)
- 浙江大学PAT_甲级_1029. Median (25)
- 浙江大学PAT_甲级_1048. Find Coins (25)
- 浙江大学PAT_甲级_1059. Prime Factors (25)
- 浙江大学PAT_甲级_1083. List Grades (25)
- 浙江大学PAT_甲级_1070. Mooncake (25)
- 浙江大学PAT_甲级_1063. Set Similarity (25)
- 浙江大学PAT_甲级_1032. Sharing (25)
- 1071. Speech Patterns (25)PAT甲级
- 【PAT甲级】1071. Speech Patterns (25)
- PAT 甲级 1071. Speech Patterns (25)
- 浙江大学PAT_甲级_1006. Sign In and Sign Out (25)
- 浙江大学PAT_甲级_1036. Boys vs Girls (25)
- 浙江大学PAT_甲级_1055. The World's Richest (25)
- 浙江大学PAT_甲级_1062. Talent and Virtue (25)
- 浙江大学PAT_甲级_1052. Linked List Sorting (25)
- UVALive 6934 Good morning! 打表判断
- 【JS/读书随笔】JavaScript编程精解/Eloquent JavaScript:Chapter 2 函数
- 二叉树的先序、中序、后序的递归及非递归实现,以及层次遍历的实现:
- 123
- Objective-C类初始化:load与initialize
- 浙江大学PAT_甲级_1071. Speech Patterns (25)
- iOS异常、错误汇总(一)
- Qt下载网址
- android--笔记--自定义view时的命名空间引用改为res-auto
- lintcode-384
- hdu 4323 编辑距离
- 稳定匹配问题
- eclipse MAT - Memory Analyzer Tool 使用进阶
- 第10章 无监督学习