Hdoj-1247-Hat's Words-dictionary tree
# Include
# Include
# Include
# Define deusing namespace std; const int N = 30; const int MAX = 50005; char word [MAX] [30]; struct node {bool temp; node * next [N]; node () {temp = false; memset (next, 0, sizeof (next); // next indicates the number of types in each layer. If it is only a lowercase letter, then 26. // if it is changed to a case-sensitive letter, it is 52. If it is added with a number, it is 62 }}; void insert (node * rt, char * s) {int I, t; I = 0; node * p = rt; while (s [I]) {t = s [I ++]-'A '; if (p-> next [t] = NULL) p-> next [t] = new node (); p = p-> next [t];} p-> temp = true; // point to the end of the word} bool search (node * rt, char s []) {int I, top; I = top = 0; int stack [MAX]; node * p = rt; while (s [I]) {int t = s [I ++]-'A '; if (p-> next [t] = NULL) return false; p = p-> next [t]; if (p-> temp & s [I]) // locate the split point containing the subword and the character has not ended. stack [top ++] = I; // import the split point to the stack, then cyclically determine whether the remaining part contains a subword} while (top) // start from these Split points and find {bool OK = true; // mark whether I = stack [-- top] is found; // The first split point p = rt; while (s [I]) {int t = s [I ++]-'A'; if (p-> next [t] = NULL) {OK = false; // No break found ;} p = p-> next [t];} if (OK & p-> temp) return true;} return false;} int main () {int I = 0; node * rt = new node (); while (gets (word [I]) {insert (rt, word [I]); I ++ ;} for (int j = 0; j