Aho_corasick automatic Machine (AC automaton)

Source: Internet
Author: User

First of all, the AC automaton is not the accept automaton, do not think that the code copied to the OJ on the automatic AC ...

Actually this thing is Aho-corasick made, so you know. So what does this thing do? • String matching problem • Multi-string matching problem * Don't you understand? Explain: For example, give a few words Acbs,asf,dsef, and then give a very long article, acbsdfgeasf, ask in this article, how many words in total, or the total number of words appear. How to achieve it, is the Kmp+trie tree. is based on the KMP algorithm, trie as the index structure of the Dongdong. So how does it relate to KMP? • The key is to add a fail pointer to the trie tree. fail The purpose of the pointer: it is like an array of next in KMP. Determines the node to be transferred when a string mismatch. The AC difficulty is the pointer algorithm, see the following so many graphs: template:
1 structaho_corasick{2     intCH[MAXN][SIGC], MS;3     intVAL[MAXN], F[MAXN], LEN[MAXN];4     intLAST[MAXN];5 Aho_corasick () {}6     voidinit () {7ms =0;8Memset (Val,0,sizeof(Val));9memset (CH,0,sizeof(CH));TenMemset (F,0,sizeof(f)); OneMemset (Last,0,sizeof(last)); Amemset (Len,0,sizeof(len)); -         return ; -     } the     voidInsertChar* S,intv) { -         intCur =0, I; -          for(i =0; S[i]! =' /'; i + +){ -             intc = S[i]-'0'; +             if(!ch[cur][c]) ch[cur][c] = + + ms;//1 -Cur =Ch[cur][c]; +         } AVal[cur] = v; Len[cur] =i; at         return ; -     } -     voidMake_fail () { -queue<int>Q; -          for(inti =0; i < SIGC; i + +)if(ch[0][i]) Q.push (ch[0][i]); -          while(!Q.empty ()) { in             intx =Q.front (); Q.pop (); -              for(intc =0; C <2; C + +){ to                 intv =Ch[x][c]; +                 if(!V) {Ch[x][c] = Ch[f[x]][c];Continue; } - Q.push (v); the                 intCur =F[x]; *                  while(cur &&!ch[cur][c]) cur =F[cur]; $F[V] =Ch[cur][c];Panax NotoginsengLAST[V] = Val[f[v]]?F[v]: last[f[v]; -             } the         } +         return ; A     } the};

Aho_corasick automatic Machine (AC automaton)

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.