[PKU 3630] string (2) {trie dictionary tree}

Source: Internet
Author: User

{

Start to discuss string issues

In the previous article, we reproduced the KMP of matrix67.AlgorithmExplanations

This article mainly discusses Trie

}

 

Trie(Pronunciation try)

IsMulti-tree for storing multiple strings

BecauseInsert and query are extremely efficientAlso knownDictionary tree

The number of forks in the tree is contained in the string.Letter count

An uppercase letter dictionary tree26.

We take this trie as an example to facilitate the discussion.

For example, you need to store 6 strings {she SHR say he HR her}

Trie is a multi-Cross Tree shown in.

I. Trie Structure

A basic trie node contains such two information.

Son ['A' .. 'Z']: pointer {Son pointerArray subscript is character set combination}

Tail: Boolean {whether the node is a wordEnd}

The character information record of trie saves some additional information on the edge (pointer) node.

The space occupied by trie is generally proportional to the total number of characters.

(Since Pascal's pointer is very weak, it is generally used belowArray analog pointer)

Ii. Trie insertion ()

In summaryInsert while reading

First, point the current pointer P to the root (note the new root during preprocessing)

Next, read each character to determine whether the current node has such a son.

If noReal-Time CreationAfter judgment, continue to go to this son, that is, P = Son [CH]

Read a stringThe tail value of the last node is true.Indicates the end of a word.

Insert TrieCode

Trie_build

  Newnode (Root );
Readln (N );
For I: = 1 To N Do
Begin
P: = Root;
While Not Eoln Do
Begin
Read (CH );
C: = Ord (CH) - 96 ;
If S [p] [C] = 0
Then Newnode (s [p] [c]);
P: = S [p] [c];
End ;
INC (G [p]);
Readln;
End ;

Iii. Trie Query

Query isRead and insert

Each character is read.If the desired son does not exist, the query fails.

If the last node is readQuery failure is not the end of the word (determined by tail)

Code and query are similar

Iv. Summary

The insertion and query time complexity of trie are linear.O (length)

But the space complexity has reachedO (K * length) or O (Σ length)

Compression trie can be used to solve this problem so that the space complexity can be reached.O (k)

I haven't even touched any places to use this method.

Lazy-tag ~

 

Finally, we provide a question to test trie.

Http://poj.org/problem? Id = 3630

The simplest trie code can solve this problem.

Direct post code

Phone

  Const  Maxc  =  10  ;
Maxn = 500000 ;
VaR T: Array [ 1 .. Maxn] Of Longint;
N: Array [ 1 . Maxn, 1 .. Maxc] Of Longint;
TT, CA, M, I, P, K, root: longint;
Flag, bool: Boolean;
Ch: Char;
Procedure Newnode ( VaR X: longint );
VaR I: longint;
Begin
INC (TT); X: = TT;
T [x]: = 0 ;
For I: = 1 To Maxc Do
N [x] [I]: = 0 ;
End ;
Begin
Assign (input, ' Plist. In ' ); Reset (input );
Assign (output, ' Plist2.out ' ); Rewrite (output );
Readln (CA );
While CA > 0 Do
Begin
TT: = 0 ;
Dec (CA );
Readln (m );
Newnode (Root );
Flag: = True;
For I: = 1 To M Do
Begin
P: = Root;
Bool: = True;
While Not Eoln Do
Begin
Read (CH );
K: = Ord (CH) - 47 ;
If N [p] [k] = 0
Then Begin
Newnode (N [p] [k]);
Bool: = False;
End ;
P: = N [p] [k];
If T [p] > 0 Then Flag: = False;
End ;
If Bool = True Then Flag: = False;
INC (T [p]);
Readln;
End ;
If Flag
Then Writeln ( ' Yes ' )
Else Writeln ( ' No ' );
End ;
Close (input); close (output );
End .

 

After discussing trie and KMP, we can consider a cool-B thing.

-- Aho-corasick Automatic MachineNow

In Article 3 and Article 4ArticleIntroduction

 

Bob Han original reprinted please indicate the source http://www.cnblogs.com/Booble/

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.