Prefix tree trie
Trie can be understood as a collection that can be quickly inserted and queried, whether the time required to insert or query is O (m)
The template is as follows:
1 Const intMaxnode = ++Ten;2 Const intSigma_size = -; 3 4 struct trie{5 intCh[maxnode][sigma_size];6 intVal[maxnode];7 intsz;8 9 voidClear () {sz=1; memset (ch[0],0,sizeof(ch[0])); }Ten intID (Charc) {returnC='a'; }; One A voidInsertChar* S,intV) {//inserting into the trie - intu=0, n=strlen (s); - for(intI=0; i<n;i++) { the intC=ID (S[i]); - if(!Ch[u][c]) { -memset (Ch[sz],0,sizeof(Ch[sz]));//initializing child node groups -val[sz]=0;//Intermediate junction val=0. +Ch[u][c]= sz++;//Create a sub-node. - } +U=CH[U][C];//go down. A } atval[u]=v; - } - - intFindChar*s) {//find if s exists - intn=strlen (s); - intu=0; in for(intI=0; i<n;i++){ - intC=ID (S[i]); to if(!ch[u][c])return false; +u=Ch[u][c]; - } the returnVal[u];//is the tail * } $};
"Summer Vacation" [utility data structure] prefix tree Trie