# Include <cstdio> # include <cstring> # include <algorithm> # include <queue> # include <string> # include <iostream> using namespace STD; struct node {int CNT; // number of records; struct node * Next [26]; node () {CNT = 0; memset (next, null, sizeof (next ));}}; void buildtrie (node * root, string s) //; {node * P = root; node * TMP = NULL; int L = S. size (); For (INT I = 0; I <L; I ++) {If (p-> next [s [I]-'a'] = NULL) {TMP = new node; P-> next [s [I]-'a '] = TMP;} p = p-> next [s [I]-'a'];} p-> CNT ++;} void findtrie (node * root, string s) // query {node * P = root; int L = S. size (); For (INT I = 0; I <L; I ++) {P = p-> next [s [I]-'a'];} printf ("% d \ n", p-> cnt-1);} void del (node * root) // Why the root value is always the value passed by the main function, this value is also used for recursion and is only executed once. This is the result of my debugging, but the code for deleting the node is like this. Yes, but I don't know why. I don't understand debugging either. I can explain it to me. /// Thank you very much {for (INT I = 0; I <26; I ++) if (root-> next [I]) del (root-> next [I]); Delete (Root);} int main () {int N; string S; node root; scanf ("% d", & N); While (n --) {CIN> S; sort (S. begin (), S. end (); buildtrie (& root, S); findtrie (& root, S); del (& root);} return 0 ;}
Questions about deleting redundant nodes in the dictionary tree