The topics are as follows:
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].
Output Specification:
for each test case, print in one line the most commonly occurring word in the input text, Followe D by a space and the number of times it had 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 a can? It can! "
Sample Output:
Can 5
The topic asks to find the most frequently occurring words from a natural segment (a line of sentences containing special characters), note the word definition here, the sequential sequence of character numbers, and the numbers are also valid . To do this, you can use Getline to read a line of sentences, and then traverse the process with StringStream to receive characters, output after processing a word, and then use map to record the frequency of the word.
① with a space to read a line, using Getline (CIN,STR);
② split the word, to read the natural segment para, from front to back traversal, using a StringStream record read the string, once read the illegal characters, the current StringStream output to a string word, to determine whether word length is greater than 0, More than 0 is a word, use map to record the number of occurrences of the current word, and according to the requirements of the topic, pay special attention to the occurrence of the word only once .
③ Note If you read the natural segment para there is only one word, there is no delimiter other than the newline character, which requires special handling because getline cannot read \ n.
Note the point:
The ①stringstream output string uses Ss.str ().
② empty stringstream do not use ss.clear, should use Ss.str ("").
The code is as follows:
#include <iostream> #include <map> #include <stdio.h> #include <sstream> #include <string > #include <algorithm>//stringstream empty using Ss.str ("")//Transformusing namespace Std;int main () {map<string, Int> words; String para; Getline (Cin,para); StringStream SS; string Word; String maxword = ""; int maxcnt =-1; bool Singleflag = true; String single; for (int i = 0; i < para.length (); i++) {char c = para[i]; if (Isalnum (c)) {SS << C; if (singleflag) {single = Ss.str (); }}else{Singleflag = false; Word = Ss.str (); if (word.length () > 0) {transform (Word.begin (), Word.end (), Word.begin (),:: ToLower); Ss.str (""); if (words.find (word) = = Words.end ()) {Words[word] = 1; int cnt = 1; if (cnt > maxcnt) { maxcnt = CNT; Maxword = Word; }else if (cnt = = maxcnt) {if (Word < Maxword) {Maxword = Word; }}}else{words[word]++; int cnt = Words[word]; if (cnt > maxcnt) {maxcnt = cnt; Maxword = Word; }else if (cnt = = maxcnt) {if (Word < Maxword) {Maxword = Word; }}}}}} if (Singleflag) {printf ("%s 1\ N ", Single.c_str (), maxcnt); }else{printf ("%s%d\n", Maxword.c_str (), maxcnt); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
1071. Speech Patterns (25)