Statistical difficulties
Problem descriptionignatius recently encountered a problem. The teacher gave him many words (only consisting of lower-case letters and no repeated words ), now the teacher asks him to count the number of words prefixed with a certain string (the word itself is also its own prefix ).
The first part of input data is a word table. Each line has one word. The length of a word cannot exceed 10. These words represent words that the teacher gave to Ignatius for statistics. A blank line indicates the end of the word table. the second part is a series of questions. Each question in each row is a string.
Note: This question only contains a group of trial data, which is processed until the end of the file.
Output provides the number of words prefixed with this string for each question.
Sample Input
Bananabandbeeabsoluteacmbabbandabc
Sample output
2310
Authorignatius. L
Solution:
This is the first time that we have made a dictionary tree. Dictionary tree Baidu encyclopedia: Also known as the word search tree, Trie tree Is Tree Structure Is a variant of the hash tree. Typical applications are used for statistics, sorting, and saving a large number Character String (but not limited to strings), so it is often used by the search engine system for text word frequency statistics. Its strength is: using the public prefix of the string to reduce the query time, minimizing unnecessary string ratio, query efficiency ratio Hash table High.
After learning the dictionary tree, we think it is very obvious that we use space for time. The space is very complex. For example, the number of dictionaries is only 26 lower-case letters, the child node of each node has 26 child nodes, and each layer of the dictionary tree retains the same letters with different words.
For better illustration, if all words only contain four letters A, B, C, and D, then the tree is created in this way.
RequirementCount the number of words prefixed with a string. dictionary tree beginners.
Code:
# Include <iostream> # include <string. h> # include <stdio. h> # include <malloc. h> using namespace STD; char STR [12]; const int maxn = 26; // The maximum number of child nodes. Assume that there are only 26 letters, you can use 26. Struct trie // tree struct {int CNT; // stores the number of occurrences of a letter trie * Next [maxn]; // The number of children corresponding to each node, assuming there are only 26 letters, you can use 26.}; trie root;/* void Init (trie t) {for (INT I = 0; I <26; I ++) T. next [I] = NULL;} * // you do not need to initialize void createtrie (char * Str) {int Len = strlen (STR) on the Root Node separately ); trie * P = & root, * q; For (INT I = 0; I <Len; I ++) {int id = STR [I]-'A '; if (p-> next [ID] = NULL) // The first encounter {q = (trie *) malloc (sizeof (trie); q-> CNT = 1; // an error is written at the beginning, which is written as Q-> CNT ++; For (INT I = 0; I <Ma XN; I ++) q-> next [I] = NULL; // initialize the child node of a non-empty node p-> next [ID] = Q; // enter P = p-> next [ID] in the tree. // at this time, P is not an empty node} else {P-> next [ID]-> CNT ++; // not the first time, number + + P = p-> next [ID] ;}} int find (char * Str) {int Len = strlen (STR); trie * P = & root; for (INT I = 0; I <Len; I ++) {int id = STR [I]-'A'; P = p-> next [ID]; // keep walking down. If (P = NULL) // The word cannot be found. At the beginning, an error was written in P-> next [ID] = NULL return 0 ;} return p-> CNT;} int main () {While (gets (STR) & STR [0]! = '\ 0') {createtrie (STR);} while (scanf ("% s", STR )! = EOF) {printf ("% d \ n", find (STR) ;}return 0 ;}