Trie (Template

Upon completion of the dictionary tree
    or template could not back ............ introduced in the code itself ----


Trie {struct 
    LL CH [N] [26], SZ, Val [N]; 
    // Val as additional information 
    // ch is here the array, the size of the second dimension is because the string 26 is composed of only lowercase letters, the first the two-dimensional size of the composition is generally determined by the character string 
    // SZ is the node number 
    Trie () { 
        SZ =. 1; // start when it is only a root node 
        memset (ch [0], 0 , sizeof ( CH [0])); 
        Memset (Val, 0, the sizeof (Val)); 
    } // this is the initialization 
    ll idx (char c) {return c-'a ';} // returns the number of the character c 
    void insert ( S * char, V LL) { 
    // insertion operation, this is the only place in the whole parts of the code pointer is used, because the function is placed inside the structure, it is best to use a pointer, in fact, equivalent to char s [] 
    // s behalf string to be inserted, v as additional information 
        LL U = 0, len = strlen (S +. 1); 
        for (I = LL. 1; I <= len; I ++) { 
            LL IDX C = (S [ I]); 
            ! IF (CH [U] [C]) {// if the insertion node does not exist, or to continue traversing down
                Memset (CH [SZ], 0, the sizeof (CH [SZ])); 
                Val [SZ] = 0; // is the intermediate node without additional information 
                ch [u] [c] = sz ++; // new node 
            } 
            u = ch [u] [c ]; // iterate down 
        } 
        Val [U] = V; // insert additional information, note that we generally only inserting additional information in the leaf node, the intermediate node typically no additional information , as a non-leaf node, different words to be used (defined) will normally be in the Trie 
    } 
} Tree;
 
 

  

 
 

 

 

 

 

Guess you like

Origin www.cnblogs.com/liuxiangyu666/p/11403513.html