1071 Speech Patterns (25 points)
People often has a preference among synonyms of the same word. For example, some may prefer "the police" and while others may prefer "the cops". Analyzing such patterns can help-narrow down a speaker's identity, which is useful when validating, for example, whethe R 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 the one test case. For each case, there are one line of text no further than 1048576 characters in length, terminated by a carriage return \n
. The input contains at least one alphanumerical character, i.e, and one character from the set [ 0-9 A-Z a-z
].
Output Specification:
For all test case, print in one line the most commonly occurring word in the input text, followed by a space and the numb Er of times it has occurred in the input. If there is more than one such words, print the lexicographically smallest one. The word should is printed in all lower case. Here a "word" was defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.
Note that words is case insensitive.
Sample Input:
Can1: "Can a can can a can? It can!"
Sample Output:
can 5
Topic: Give a string to find out the most occurrences of word, the word is composed of alphanumeric. and makes case insensitive.
The following code is AC on the Ox-guest online, but the last Test point on Pat didn't pass and the answer was wrong.
#include <iostream>#include<cstdio>#include<map>using namespacestd;stringLowcase (strings) { for(intI=0; I<s.size (); i++){ if(s[i]>='A'&&s[i]<='Z') {S[i]=s[i]-'A'+'a';//How do I convert uppercase to lowercase? } } returns;}intMain () {strings=""; Map<string,int>MP; CharCh=GetChar (); while(ch!='\ n'){ if(IsDigit (ch) | |isalpha (CH)) {s+=ch;//after the string is added. }Else { if(s!="") {mp[lowcase (s)]++;//Add to map//cout<<s<< "";s="";//is assigned an empty value. }} Ch=GetChar (); } intmx=0; for(Auto It=mp.begin (); It!=mp.end (); it++){ if(it->second>MX) {MX=it->second; S=it->First ; }} cout<<s<<" "<<MX; return 0;}
Sure enough, I found the problem:
The running result should be 3, not 2.
#include <iostream>#include<cstdio>#include<map>using namespacestd;stringLowcase (strings) { for(intI=0; I<s.size (); i++){ if(s[i]>='A'&&s[i]<='Z') {S[i]=s[i]-'A'+'a';//How do I convert uppercase to lowercase? } } returns;}intMain () {strings=""; Map<string,int>MP; CharCh=GetChar (); while(ch!='\ n'){ if(IsDigit (ch) | |isalpha (CH)) {s+=ch;//after the string is added. //because map doesn't go in. }Else { if(s!="") {mp[lowcase (s)]++;//Add to map//cout<<s<< "<<mp[s]<< ' \ n ';s="";//is assigned an empty value. } } //cout<<ch;Ch=GetChar (); if(ch=='\ n') {//Plus this judgment is AC ~ ~ ~if(s!="") Mp[lowcase (s)]++;//because if the last one doesn't count. } } intmx=0; //cout<< ' \ n '; for(Auto It=mp.begin (); It!=mp.end (); it++){ if(it->second>MX) {MX=it->second; S=it->First ; }} cout<<s<<" "<<MX; return 0;}//can can
Learned, this string processing ~ ~ ~
PAT 1071 Speech patterns[General]