AC Automatic Machine

Source: Internet
Author: User

AC Automatic Machine

The direct study of AC automata is difficult to understand, it is highly recommended to learn the KMP and the dictionary tree and after a certain exercise, for the mismatch pointer and the dictionary tree structure have a certain understanding of the content of the AC automata. A detailed introduction to AC automata can be found in Rujia's "algorithmic Competition Introductory Classic Training Guide" P214.

Give you a dictionary ( containing n non-repeating words ) and then give you a string of contiguous text ( long as len), ask you which locations in the text appear to be one or several words in the dictionary exactly? Output these locations and the words that appear.

This problem can be done with n-th KMP algorithm ( efficiency is O (n*len* Word average length ) ), you can also use 1 dictionary trees to match each letter position of a text string ( efficiency is O (len* Average depth per dictionary tree traversal ) )。 The above two methods are not efficient, if the use of AC automata to solve, The efficiency will be linear O (len) time complexity. (The above 3 time efficiency did not build KMP, dictionary tree and AC the time of the automaton is counted. )

KMP algorithm specifically solves the problem of single template matching for long text , and the dictionary tree solves The problem of multi-template matching of single word ( short text ) . The multi-template matching problem of long text is solved by AC automata. And the AC automaton not only has the advantage in time, but also has the superiority in the space.

The template code is as follows:

<span style= "FONT-SIZE:18PX;" > #include <cstdio> #include <cstring> #include <queue>using namespace std;const int maxnode=11000;    const int sigma_size=26;struct ac_automata{int ch[maxnode][sigma_size];   int Val[maxnode];     Each string has an end node that has a non-0 Val int f[maxnode];  Fail function int Last[maxnode];    The word represented by the Last[i]=j table J node is the suffix of the I node word, and the J node is the word node int sz;        Initializes information about the root node of number No. 0, void init () {sz=1;        memset (ch[0],0,sizeof (ch[0));    val[0]=0;        }//insert is responsible for constructing CH with Val Array//insert string, V must not 0 represents a word node void insert (char *s,int v) {int N=strlen (s), u=0;            for (int i=0; i<n; i++) {int id=s[i]-' a ';                if (ch[u][id]==0) {ch[u][id]=sz;                memset (ch[sz],0,sizeof (Ch[sz));            val[sz++]=0;        } U=ch[u][id];    } val[u]=v;        The//getfail function is responsible for constructing the F and last array void Getfail () {queue<int> q;        last[0]=f[0]=0; for (inti=0; i<sigma_size;            i++) {int u=ch[0][i];                if (u) {f[u]=last[u]=0;            Q.push (U);            }} while (!q.empty ())//in BFS order, fail {int r=q.front (); Q.pop ();                for (int i=0; i<sigma_size; i++) {int u=ch[r][i];                if (u==0) continue;                Q.push (U);                int V=f[r];                while (v && ch[v][i]==0) v=f[v];                F[u]= Ch[v][i];            Last[u] = Val[f[u]]?f[u]:last[f[u]];            }}}//recursively print prefix node number identical to node I suffix//enter this function to ensure that val[i]>0 void print (int i) {if (i) {            printf ("%d\n", I);        Print (Last[i]);        }}//In S to find out which several template words have been found in void find (char *s) {int N=strlen (s), j=0;            for (int i=0; i<n; i++) {int id=s[i]-' a ';            while (J && ch[j][id]==0) j=f[j];        J=ch[j][id];    if (Val[j]) print (j);        else if (Last[j]) print (last[j]); }    }}; Ac_automata ac;</span>

AC basic application of automatic machine

UVA 1449 dominating Patterns (AC automaton): Note that there is a case for repeating word input in the input. problem Solving report!

HDU 2222 Keywords Search (ac automaton): give you a text and multiple words and ask how many words you have appeared. Note that words may be duplicated. Problem Solving report !

HDU 2896 virus Attack (AC automaton): Basic application of AC automata. Problem Solving report!

HDU 3065 Virus attack continuous (AC automaton): Basic application of AC automata. Problem Solving report !

ZOJ 3430 Detect the Virus (AC Automaton + string conversion): The trouble is that you need to convert the 64-bit encoding to a string before matching the AC automaton. Problem Solving report !

ZOJ 3228 Searching the string (AC automaton): How can I find the maximum number of occurrences when the same template string is not overlapped? Problem Solving report !

AC Automatic Machine +DP

UVA 11468 Substring (AC automaton + probabilistic DP): A modified AC automaton is required and the realization of the probability DP is noted. problem Solving report!

POJ 2778 DNA Sequence (ac automaton + Matrix power DP): The recursive formula needs to be obtained by DP, then the recursive matrix is found, the problem is a matrix power operation. Problem Solving report !

HDU 2243 Entrance Examination Road boundless-word complex (AC automaton + matrix Power): POJ2778 of the enhanced version. Problem Solving report !

POJ 1625 censored! (AC automaton +DP): Similar to POJ2778, but does not require a matrix power. problem Solving report!

HDU 2825 Wireless Password (AC automaton + state compression DP): The string is still generated, but this time it contains exactly k different template words. Problem Solving report !

HDU 2296 Ring (ac automaton +DP): Note The value of the match array. problem Solving report!

HDU 2457 DNA Repair (ac automaton +DP): Very ingenious DP definition. problem Solving report!

HDU 3341 Lost ' s Revenge (AC automaton +DP): Alternative state compression DP problem, later remember so compressed state. problem Solving report!

AC Automatic Machine

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.