/*************************************** * ******************* Data structure: the trie tree, also known as the word search tree or dictionary tree, is a tree structure and a variant of the hash tree. Basic principle: the core idea of the trie tree is to change space time, the common prefix of a string is used to reduce the query time overhead to improve efficiency. Application: used to count and sort a large number of strings (but not limited to strings ), therefore, it is often used by the search engine system for text word frequency statistics. Advantage: to minimize unnecessary string comparisons, the query efficiency is higher than that of the hash table. Basic Features: (1) root nodes do not contain characters, each node except the root node contains only one character. (2) from the root node to a node, the character passing through the path is connected to the corresponding string of the node. (3) all subnodes of each node contain different characters; **************************************** * ******************/# include <iostrea M> # include <cstring> # include <cstdlib> # include <cstdio> # include <climits> # include <algorithm> using namespace STD; const int max = 26; struct trie // trie node declaration {bool isstr; // mark whether the node constitutes a trie * Next [Max]; // a pointer array, hold pointer to each son node}; void insert (trie * root, const char * s) // insert word s into the dictionary tree {If (root = NULL | * s = '\ 0') return; trie * P = root; while (* s) {If (p-> next [* s-'a'] = NULL) // If the node that stores the character does not exist, then create the node {trie * temp = new trie; f Or (INT I = 0; I <Max; I ++) {temp-> next [I] = NULL;} temp-> isstr = false; p-> next [* s-'a'] = temp; P = p-> next [* s-'a'];} else {P = p-> next [* s-'a'];} s ++; // Let the pointer s point to the next character} p-> isstr = true; // mark the end of a word here to form a string} int search (trie * root, const char * s) // query whether a word s already exists {trie * P = root; while (P & * s) {P = p-> next [* s-'a']; s ++;} return (P & P-> isstr); // The word exists only when it is marked as true at the end of the word} void del (trie * root) // release the heap space occupied by the entire dictionary tree {for (INT I = 0; I <m Ax; I ++) {If (root-> next [I]! = NULL) {del (root-> next [I]) ;}} Delete root;} int main () {// freopen ("C: \ Users \ Administrator \ Desktop \ kd.txt "," r ", stdin); char s [100]; trie * root = new trie; for (INT I = 0; I <Max; I ++) {root-> next [I] = NULL;} root-> isstr = false; int n, m; // n is the number of words entered in the trie tree, and m is the number of words to be searched scanf ("% d", & N); getchar (); for (INT I = 0; I <n; I ++) // create a dictionary tree {scanf ("% s", S); insert (root, S );} while (~ Scanf ("% d", & M) {If (! M) break; For (INT I = 0; I <m; I ++) // query {scanf ("% s", S); If (search (root, s) printf ("Yes \ n"); else printf ("NO \ n");} printf ("\ n");} del (Root ); // It is important to release space. Return 0 ;}