Problem descriptionignatius recently encountered a problem. The teacher gave him many words (only lowercase letters are used and no duplicate words will appear ), the teacher asks him to calculate 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 set of test data, which is processed until the end of the file.
Output provides the number of words prefixed with this string for each question.
Sample inputbanana
Band
Bee
Absolute
ACM
Ba
B
Band
Abcsample output2
3
1
0
The teacher gave him a lot of words (only lowercase letters, no repeated words), and now the teacher asked him to count the number of words prefixed with a string.
Solution: use the dictionary tree to store the given words and search for them ..... Apply the dictionary tree template directly...
Question connection: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1251
1 # include <stdio. h> 2 # include <string. h> 3 # include <stdlib. h> 4 # include <malloc. h> 5 # define Max 26/* 26 letters */6 typedef struct node 7 {8 struct node * child [Max];/* store the next character */9 int N; /* record the number of times the current word appears */10} node, * node; 11 node root;/* The root node of the dictionary tree (no character is stored) */12 Void insert (char * Str)/* Insert the word */13 {14 int I, index, Len; 15 node T = NULL, newnode = NULL; 16 Len = strlen (STR); 17 T = root;/* the current node is Root Node */18 for (I = 0; I <Len; I ++)/* Insert characters one by one */19 {20 Index = STR [I]-'A '; /* obtain the subscript of this character */21 if (t-> child [Index]! = NULL)/* The character is already in the dictionary tree */22 {23 t = T-> child [Index]; /* modify the current node location */24 (t-> N) ++;/* The current word appears again, accumulate */25} 26 else/* this character has not appeared, then add node */27 {28 newnode = (node) calloc (1, sizeof (node )); /* Add a node and initialize */29 T-> child [Index] = newnode; 30 t = newnode; /* modify the current node location */31 T-> n = 1; /* this new word appears */32} 33} 34} 35 int find_word (char * Str)/* search for the word in the dictionary tree */36 {37 int I, index, Len; 38 node T = NULL; 39 Len = strlen (STR); 4 0 T = root;/* Search Start from the root node */41 for (I = 0; I <Len; I ++) 42 {43 Index = STR [I]-'A';/* obtain the subscript of this character */44 If (t-> child [Index]! = NULL)/* The current character exists in the dictionary tree */45 t = T-> child [Index];/* modify the location of the current node */46 else47 return 0; /* the word does not match after comparison. The dictionary tree does not contain the word */48} 49 return T-> N; /* Number of times this word appears */50} 51 void DELE (node root)/* release memory */52 {53 int I; 54 if (null = root) 55 return; 56 57 for (I = 0; I <Max; I ++) 58 If (root-> child [I]! = NULL) 59 DELE (root-> child [I]); 60 free (Root); 61 root = NULL; 62} 63 int main () 64 {65 char TMP [11]; 66 root = (node) calloc (1, sizeof (node); 67 while (gets (TMP) & strlen (TMP )) 68 insert (TMP); 69 while (scanf ("% s", TMP )! = EOF) 70 printf ("% d \ n", find_word (TMP); 71 DELE (Root); 72 return 0; 73}Code