# PAT(A) 1071. Speech PATterns (25)

### 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

### 代码

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 `#include "iostream" #include "algorithm" #include "map" #include "vector" #include "string" using namespace std; char isAlpha(char c){ if(c<='9' && c >= '0') return c; if(c<='z' && c >= 'a') return c; if(c<='Z' && c >= 'A') return c-('A' - 'a'); return 0; } bool comp(const pair &a,const pair &b){ return a.second>b.second; } int main(){ map mm; map::iterator it; char c; string s = ""; while(scanf("%c",&c)){ if(c=='\n') break; c = isAlpha(c); if (c) s += c; else{ if(s.size()){ if(mm.count(s)){ mm[s] ++; }else{ mm[s] = 1; } } s = ""; } } // 处理如果最后是可行字符的情况 if(s.size()){ if(mm.count(s)){ mm[s] ++; }else{ mm[s] = 1; } } vector> v(mm.begin(),mm.end()); sort(v.begin(),v.end(),comp); cout<