Dictionary tree Trie, Dictionary trie

Source: Internet
Author: User

Dictionary tree Trie, Dictionary trie

Dictionary tree Trie

The dictionary tree is also called the word search tree (Trie) or Prefix Tree (See Liu rujia's Guide to getting started with algorithm competitions, P208). As the name suggests, it is related to the word prefix. Here is a dictionary tree composed of a word and a dictionary. You can determine whether the word belongs to the dictionary within O (m) (m is the length of the given word. However, if you use KMP or other brute-force methods, you must at least traverse all words in the dictionary.

It is a dictionary tree with the words abc, abcd, B, bcd, efg, and hix.

The static template of the dictionary tree is given below. The so-called static template is because we have applied for enough nodes to store the entire dictionary tree. We need to estimate how many nodes are needed.

Of course, the dictionary tree also uses the dynamic version implemented by pointers. The dynamic version facilitates the deletion of words in the dictionary tree, but if you do not need the current tree after each build, remember to recursively delete every pointer of the dictionary tree, otherwise the memory is leaked.

We use the vector <26 element array> ch; in this form, we can also implement a dynamic dictionary tree version without manually deleting the pointer to reclaim the memory.

The dictionary tree is advantageous in determining whether a word belongs to a dictionary. However, the dictionary tree is insufficient in determining the number of words in a text dictionary, because each character of a text string must go through the dictionary tree (It is similar to the brute force method used for pattern string matching.). In this case, you should use the AC automatic machine (The following is a summary of the AC automatic mechanism. Similar to the KMP algorithm used for pattern string matching).


Dictionary tree static template:

# Define MAX 26 const int maxnode = 4000*100 + 100; // estimate the maximum number of nodes in the dictionary tree. const int sigma_size = 26; // The maximum number of sons for each node struct Trie {// here ch is implemented using the array of vector <26 elements> ch, it can achieve dynamic memory int ch [maxnode] [sigma_size]; // ch [I] [j] = k indicates that the son of node j of node I is the node k int val [maxnode]; // val [I] = x indicates that the weight of node I is x int sz. // The dictionary tree contains a total of sz nodes, from 0 to sz-1 labels // initialize void clear () {sz = 1; memset (ch [0], 0, sizeof (ch [0]); // If the ch value is 0, there is no son.} // The return character c should correspond to the Son Number int idx (char c) {return c-'A ';} // insert the word s into the dictionary tree, however, if the word s already exists, it will be inserted repeatedly and overwrite the weight. // before executing the insert operation, you need to determine whether the word s already exists. void insert (char * s) {int u = 0, n = strlen (s); for (int I = 0; I <n; I ++) {int id = idx (s [I]); if (ch [u] [id] = 0) // This son does not exist {ch [u] [id] = sz; memset (ch [sz], 0, sizeof (ch [sz]); val [sz ++] = 0;} u = ch [u] [id];} val [u] = n ;} // search for the word s bool search (char * s) {int n = strlen (s), u = 0; for (int I = 0; I <n; I ++) {int id = idx (s [I]); if (ch [u] [id] = 0) return false; u = ch [u] [id];} return val [u] ;}}; Trie trie; // define a dictionary tree

Dictionary tree Application

HDU 1251 statistical difficulties (Dictionary tree Trie): Find the number of all words prefixed with string s.Solution report!

Ultraviolet A 1401 Remember the Word (DP + dictionary tree Trie): How many methods can a string be made up of words in the dictionary?Solution report!

HDU 1671 Phone List (Dictionary tree Trie): Is there a prefix of another string in the dictionary?Solution report!

HDU 1247 Hat's Words (Dictionary tree Trie): word matching.Solution report!

POJ 1056 IMMEDIATEDECODABILITY (Dictionary tree Trie): Search for prefix words.Solution report!

HDU 4099 Revenge of Fibonacci (high-precision addition + dictionary tree Trie): basic application of the dictionary tree, but involves large integers, so pay attention to more details.Solution report!

POJ 2001 Shortest Prefixes (Dictionary tree Trie): the unique prefix of the output word. If there is no unique prefix, the word is output directly.Solution report!

POJ 2503 Babelfish (Dictionary tree Trie): basic application of the dictionary tree.Solution report!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.