Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1251
The meaning of the question has been quite clear: count the number of words prefixed with a certain string (the word itself is also its own prefix ).
Now, I have directly provided some comments to the basic template of the dictionary tree. I hope you can understand it.
# Include <iostream> # include <cstring> using namespace STD; typedef struct tree // create a node {int num; struct tree * BR [26]; // each layer has 26 letters} node; node * head; void tree_insert (char STR []) // build {node * t, * s = head; int I, j; int Len = strlen (STR); for (I = 0; I <Len; I ++) {int id = STR [I]-'A '; if (S-> Br [ID] = NULL) {T = new node; // create a new node under this letter for (j = 0; j <= 25; j ++) // initialize the new node {T-> Br [J] = NULL;} t-> num = 0; s-> Br [ID] = T; // sub-tree pointer marking the node link} s = s-> Br [ID]; // s points to the next node S-> num ++; // number of times this vertex has passed} int tree_find (char STR []) // the search process, search {node * s = head; int count; int Len = strlen (STR); For (INT I = 0; I <Len; I ++) from each letter) {int id = STR [I]-'A'; If (S-> Br [ID] = NULL) {COUNT = 0; return count ;} else {S = s-> Br [ID]; Count = s-> num;} return count;} int main () {int I; head = new node; // header node for (I = 0; I <= 25; I ++) // 26 nodes, initialize {head-> Br [I] = NULL; head-> num = 0;} Char TEM [15]; while (gets (TEM), strcmp (TEM, "") {tree_insert (TEM ); //} while (gets (TEM) {cout <tree_find (TEM) <Endl;} return 0 ;}