[AC automation] dynamic planning based on the state design of Automation

Source: Internet
Author: User

AC automatic AC Machine, ah, hope so...

 

After learning about the working principle of the AC automatic machine, I have to make a sigh of relief. This is a very beautiful algorithm, either in time, space, or constant, they all have outstanding performance.

The basic operation of an AC automatic machine is multi-String Matching. The simplest idea is to run the n-edge KMP, And the complexity is O (Nm). The AC automatic machine can be resolved in O (m.

 

I am very grateful to this Daniel for his introduction of the AC automatic machine. Here I am very clear about it. The AC automatic machine is the combined version of the dictionary tree and KMP. Here I will post my own AC automatic machine code:

 

Program acauto; const maxn = 250000; <br/> var <br/> P: array [0 .. maxn, 'A '.. 'Z'] of longint; <br/> L, Q, C: array [0 .. maxn] of longint; <br/> I, J, K, beg, Clo, n, m, T, Ans: longint; <br/> A, B: ansistring; x: Char; <br/> begin <br/> readln (n, m); <br/> readln (a); T: = 1; <br/> for X: = 'A' to 'Z' do P [0, x]: = 1; <br/> for I: = 1 to n do begin <br/> readln (B); K: = 1; <br/> for J: = 1 to length (B) do begin <br/> If P [k] [B [J] = 0 then begin <br/> Inc (t ); P [k] [B [J]: = T; <br/> end; <br/> K: = P [k] [B [J]; <br/> end; <br/> C [k]: = 1; <br/> end; <br/> beg: = 1; clo: = 1; Q [1]: = 1; <br/> while beg <= clo do begin <br/> I: = Q [beg]; <br/> for X: = 'A' to 'Z' do begin <br/> T: = L [I]; <br/> If P [I] [x] = 0 then continue; <br/> while P [T] [x] = 0 do T: = L [T]; <br/> L [p [I] [x]: = P [T] [X]; <br/> Inc (ClO); Q [clo]: = P [I] [X]; <br/> end; <br/> Inc (BEG); <br/> end; <br/> I: = 1; C [0]: =-1; <br/> for J: = 1 to M do begin <br/> while P [I] [A [J] = 0 do I: = L [I]; <br/> I: = P [I] [A [J]; T: = I; <br/> while C [T] <>-1 do begin <br/> Inc (ANS, c [T]); C [T]: =-1; <br/> T: = L [T]; <br/> end; <br/> writeln (ANS); <br/> end. <br/>

 

 

I searched for the questions about the AC automatic mechanism on the Internet and found these two questions: poj1_1 and poj2778. What makes me amazing is that they are actually dynamic planning.

 

After these two questions, I found that the original AC automatic machine has more magical and wonderful properties to mine.

First, briefly describe 3691 and give you some strings and an article, so that this article does not contain these strings with the minimum number of modifications.

DP is something you can think of soon, but state compression is the key. If a number is used to represent the State, it will be far beyond the range. At this time, we found that we do not need to know what the main string looks like, as long as we know the degree of matching, the AC automatic mechanism also satisfies the non-aftereffect nature (the matching state is only related to the string that you are currently visiting and that needs to be matched later), so we can design a very beautiful state:

F [I, j] indicates that the I-th character is scanned, and the minimum cost for going to node J on the automatic machine is clear. Do not change it, or change it to another character, the limit cannot be reached.

Poj2778 is a DP that requires matrix optimization. It is similar to the previous question. After the status is designed, the matrix DP can be constructed. The complexity of the matrix is O (N3 ), one optimization is to remove some redundant states to reduce the time consumption of Matrix Multiplication. Obviously, if a node is a successfully matched node, you can ignore it.

 

There should be more magical functions available in the AC automatic mechanism to be explored ....

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.