Application of Trie

Source: Internet
Author: User
This post was last edited by xuzuning on 2013-04-14 19:02:03

Should CSDN request, received CSDN Moon cake should be divergent sub-paste
The former post has been sent due to go round. Therefore, the knot paste, than continue to post scattered points

Class Ttrie {protected $buffer = array ();  Protected $dict = Array (array ()); protected $input = 0; String current offset protected $backtracking = 0;  String backtracking position public $debug = 0;  public $savematch = 1; function set ($word, $action = ') {if (Is_array ($word)) {foreach ($word as $k + = $v) $this->set ($k, $v); return;} $p = count ($this->dict); $cur = 0; Current node number foreach (Str_split ($word) as $c) {if (Isset ($this->dict[$cur [$c])) {//already exists, move down $cur = $this->dict[$cur] [$c] ; continue;} $this->dict[$p]= Array (); Create a new node $this->dict[$cur] [$c] = $p; The parent node records the child node number $cur = $p; Sets the current node as the newly inserted $p++;} $this->dict[$cur [' acc '] = $action;  A word ends, tag leaf node} function match ($s) {$ret = array (); $cur = 0;//current node, initial root node $i =& $this->input;//String current offset $p =& $this->backtracking; string backtracking position $s. = "n"; Additional Terminator $len = strlen ($s), $buf = ", while ($i < $len) {$c = $s {$i};if (isset ($this->dict[$cur] [$c])) {//if there is $cur = $th is->dict[$cur] [$c]; Go to the corresponding position if (Isset ($this->dict[$cur [$s [$i +1]])) {//check next character isNo also can match, length first $i++;continue;} if (Isset ($this->dict[$cur [' ACC '])) {//Is leaf node, Word match! if ($buf! = ") {$this->buffer[] = $buf; $buf =";} if ($this->savematch) $this->buffer[] = substr ($s, $p, $i-$p + 1); Remove the matching position and match the word $ar = explode (', ', $this->dict[$cur [' acc ']); Call_user_func_array (Array ($this, Array_shift ($ar)), $ AR); $p = $i + 1; Set the next backtracking position $cur = 0; Resets the current node to the root node}} else {//mismatch $buf. = $s {$p};//substr ($s, $p, $i-$p + 1);//Save unmatched location and unmatched content $cur = 0;//reset current node to root node $i = $p; /set current offset to backtracking position $p = $i + 1; Set next backtracking position} $i + +;  The next character}if (Trim ($buf, "s")) $this->buffer[] = Trim ($buf, "n"); return $this->buffer; } function __call ($method, $param) {if ($this->debug) printf ("Offset:%d backtracking:%d\n", $this->input, $this-  backtracking); }}


Reply to discussion (solution)

This is the National Day mid-Autumn Festival welfare ...



The eldest brother in each should I did not spread sub-posts AH? Ha ha... I went to the water area early. Hey, whoa.
Continue to receive points

Beginners are definitely useful.

Access Points ~ ~ ~
Which is the moon cake?

I don't see what I know.

What's this code for? Not understand ...
Read the wiki explanation, actually still do not understand ... Http://zh.wikipedia.org/wiki/Trie
The University white read ...

Didn't learn the algorithm, see the tree will know to crawl

The text ...

What's this code for? Not understand ...
Read the wiki explanation, actually still do not understand ... Http://zh.wikipedia.org/wiki/Trie
The University white read ...
The dictionary tree, first read into the text (multiple strings), and then find a string in the text appeared several times and other related applications ...
The complexity is equal to the length of the string ....
Advantages: Fast Speed
Cons: Large space costs

Thanks, thank you.

Seriously support this spirit of sharing, there will be a sharing of progress

Ewn

Collection of Hey

Collection!

Take points, Boss Mighty!

Tongs Liu Ming

Thanks, Xu Go.

Xu Go Thank you.

A long time did not come, since I saw scattered points, decisive answer

Take the mooncakes, study, collect

Next, Mark, take a slow look.

Decisive replies to get points!

Beginners are definitely useful.

What's this code for? Not understand ...
Read the wiki explanation, actually still do not understand ... Http://zh.wikipedia.org/wiki/Trie
The University white read ...

I remember the content of discrete or compiled principles.

Take the moon cakes with a decisive answer.

Good stuff!

Algorithm sticker ... Feel C + + write lot better ...

Why is my sofa post deleted, and no notice? Dark?

Roger that! Access points

Good man ah, thank you

The dictionary tree, first read into the text (multiple strings), and then find a string in the text appeared several times and other related applications ...
The complexity is equal to the length of the string ....
Advantages: Fast Speed
Cons: Large space costs
Thank you, now a little understand, do a model, practical understanding to go.

How to use AH. I can't read it. Landlord can give a few examples to see?

Continuous access to ING

Kan kan

The Mid-Autumn Festival to meet the points

Congratulations, congratulations, to pick up the points!

Weidejif Zhihao Huitiel

Study under

Learn to be under ing ...

Learn ing

Is this a hair moon cake?

Follow Daniel.

I got a split here.

I can't read it.

Alas

It's sad.

Moderator would like to ask

$TTrie = new Ttrie ();
$TTrie->set (' abc ');
$TTrie->set (' Abd ');
Var_dump ($TTrie->dict);

Array (5) {
[0]=>
Array (1) {
["A"]=>
Int (1)
}
[1]=>
Array (1) {
["B"]=>
Int (2)
}
[2]=>
Array (2) {
["C"]=>
Int (3)
["D"]=>
Int (4)
}
[3]=>
Array (1) {
["ACC"]=>
String (0) ""
}
[4]=>
Array (1) {
["ACC"]=>
String (0) ""
}
}


What is the use of the ACC is not their own and C, D peer to the same.

Study, Hey, nice

  • 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.