Talking about AC automata from trie

Source: Internet
Author: User

Zjoi Sam let me deeply hit, WJZ big God wrath D Chen Teacher's T3 is Sam naked title Orz ... How am I going to get mixed up? "from Trie talk about AC automata" cheat cheat experience.

Trie

Trie is a fun data structure. Each of its nodes is a letter, hence the name ' Letter tree '.

Out a picture to let everyone feel the next.

(Image powered by Saibu NAOCU)

It's a tree that's plugged in.

Ape,app,applicant,application,bake,ban,banana

The trie of such words. The red node indicates the accepted state.

Obviously, the search simply follows the chain, inserting only the edges to find the edges.

(delete simply removes the accepted state, or at this point it is deleted to its most recently accepted parent node when it has no child nodes.)

understood good writing efficiency.

AC automatic Machine

AC automata is a data structure based on trie. It is a real automaton.

The AC automaton, in short, is a trie with some strange things added.

(Powered by cocoa ....) CACOO)

The solid line represents the path on the trie, and the dashed lines represent the fail pointer.

What is the fail pointer? When you are on this node, the path you want to take when the next character is mismatched. Very similar to the next array of KMP.

Its definition is basically the same. The longest suffix with the same prefix (the pointer to the last letter node of the prefix).

It's much the same. The calculation is the same. Jump to the S[p-1 (Next[k]) fail until you can match.

Simple, huh? It's like KMP.

So the question is again. How to output it?

Let's draw a picture and think about it.

Originally, follow the fail pointer to go down can ah ...

Then we have an algorithm that matches the AC automaton.

Match the first ' a ' and look down from root

Find a match, a match plus one. Look down for the next character to find null.

The fail pointer jumps back to root. Look for the character C.

Look for the character B. is not the accepted state, continue.

A is a receiving state. Walk back along the fail pointer to output the result.

-------------------

Matches C-fail-jump.

------------------------------

...........................

................... Eventually

Results

construct an AC automaton

So, how to construct an AC automaton?

It is obvious that you can go along with the fail pointer of the father at each join, go to the first found node with the same character clause, and point the fail to that node, and if there is no same character node in root, fail points to root.

This is an online approach. Offline Of course can be solved with BFS, so that when a node is processed, its father that layer of the node has been processed. The Fail node is only recently possible on the father's level. The complexity should be the same as the online approach.

Update: Made a mistake, ac automaton is not online. Cause the fail pointer may change.

Chart Address: Https://cacoo.com/diagrams/xqj6UFk5zcllgHGW

Talking about AC automata from trie

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.