PAT_ADVANCED LEVEL 1071 C++ SLT MAP 小记

来源:互联网 发布:android程序员简历 编辑:程序博客网 时间:2024/05/07 13:23

1071. Speech Patterns (25)

时间限制
300 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
HOU, Qiming

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,可是Map大二学过去已经两年了,重新复习之,下边为记录:
Map和multimap将key/value pair 当初元素,进行管理,它们可以根据key的排列准则将元素进行自动排序。mutimap允许重复元素,maps不允许。
If you wanna use "map" and "multimap", must include the head file <map>: #include<map>
namespace std{
template <class Key, class T, class Compare = less<Key>, class Allocator = allocator<pair<const Key, T> > class map;
}
NOTE:目前还是有个问题不知道,直接用getline(输入还是不对)。而是用while(scanf("%c",&ch) && ch!= '\n')这种形式,而且后边还需要加上push_back('\n'),这到底是为什么始终想不通,f**k. 算啦,以后说不定会顿悟,这个问题需要记住,自己不知道的东西还是太多太多了。。
AC代码:
#include<iostream>#include<map>#include<vector>#include<string>#include<algorithm>using namespace std;bool is_alpha_num(char &ch){if(ch >= '0' && ch<='9'){return true;}else if(ch >= 'a' && ch <= 'z'){return true;}else if(ch >= 'A' && ch <= 'Z'){int delta = 'A' - 'a';char newCh = ch - delta;ch = newCh;return true;}else{return false;}}void split_to_map(vector<char> input,map<string, int> &chars){string stringChar = "";for(int i = 0; i<input.size(); i++){if(is_alpha_num(input[i])){stringChar += input[i];}else{if(is_alpha_num(stringChar[0])){chars[stringChar]++;}stringChar = "";}}}struct Word{int occured;string word;Word(int occured,string str){this->occured = occured;this->word = str;}};bool cmp(Word word1,Word word2){if(word1.occured != word2.occured){return word1.occured > word2.occured;}else{return word1.word < word2.word;}}int main(){char ch;vector<char> inputString;string input;map<string,int> chars;while(scanf("%c",&ch) && ch != '\n'){inputString.push_back(ch);}inputString.push_back('\n');vector<Word> words;split_to_map(inputString,chars);int maxNum = 0;string maxString = "";vector<string> characters;for(map<string,int>::iterator  pos= chars.begin(); pos!= chars.end(); pos++){Word word = Word(pos->second,pos->first);words.push_back(word);}sort(words.begin(),words.end(),cmp);cout<<words[0].word<<" "<<words[0].occured<<endl;return 0;}


0 0
原创粉丝点击