Click here to transfer to HDU 1251
* * Statistical puzzle **problem description ignatius recently encountered a problem, the teacher gave him a lot of words (only lowercase letters, no duplicate words appear), Now the teacher asked him to count the number of words prefixed by a string (the word itself is also its own prefix). input the first part of the input data is a list of words, one word per line, and the length of the word does not exceed 10 , They represent the words that the teacher handed to ignatius , and a blank line represents the end of the Word table. The second part is a series of questions, one question per line, and each question is a string. Note: The subject has only one set of test data, processing to the end of the file. output for each question, give the number of words prefixed with the string. sample input bananaband Beeabsoluteacmbabband abcsample Output 2 3 1 Span class= "Hljs-number" >0
Problem-Solving ideas: I originally wanted to use the trie tree, but I think the code is too long, and I do not quite understand, and so I will when I fill in, and now use the map function in the STL:
Directly on the code bar:
#include <iostream>#include <cstring>#include <cstdio>#include <map>using namespace STD;Charstr[ -];intMain () { Map<string, int>M while(Gets (str)) {intLen =strlen(str);if(!len) Break; for(intI=len; I>0; i--) {Str[i] =' + '; m[str]++; } } while(Gets (str)) {printf("%d\n", M[str]); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1251 Statistical puzzles