LA 4670 (AC automaton template problem) dominating Patterns

Source: Internet
Author: User

AC Automaton big name Aho-corasick automata, do not know the thought can be automatic AC, although it does help you to ac some problems. =_=||

AC automata for several days, the function is multiple pattern strings in the text string matching.

Because there are multiple pattern strings that make up a tire tree, it cannot be the same as the previous linear recursive mismatch function f, so it is changed into BFS to find the mismatch function.

The last array (suffix link) on petition means that the word node is not encountered on a branch of the tire tree, but a word may be the suffix of a string that has already been matched.

Give me a chestnut:

There are two pattern strings: AAABBB, AB

Now that the AAAB has been matched, the position is now tire the AAABBB branch of the tree, obviously not encountering the word node, but notice that AB is the suffix of the aaab, that is to say we did not match the first pattern string, but found the second pattern string ab.

This is how the last array works.

1#include <cstdio>2#include <string>3#include <cstring>4#include <queue>5#include <map>6 using namespacestd;7 8 Const intSigma_size = -;9 Const intMaxnode =11000;Ten Const intMaxs = Max+Ten; One  Amap<string,int>MS; -  - structAhocorasickautomata the { -     intCh[maxnode][sigma_size]; -     intF[maxnode];//a mismatch function -     intVal[maxnode]; +     intLast[maxnode]; -     intCNT[MAXS]; +     intsz; A  at     voidInit () -     { -memset (ch[0],0,sizeof(ch[0])); -memset (CNT,0,sizeof(CNT)); - ms.clear (); -SZ =1; in     } -  toInlineintIdxCharc) {returnC'a'; } +  -     //Insert string s, V > 0 the     voidInsertChar* S,intv) *     { $         intU =0, n =strlen (s);Panax Notoginseng          for(inti =0; I < n; i++) -         { the             intc =idx (s[i]); +             if(!Ch[u][c]) A             { thememset (Ch[sz],0,sizeof(Ch[sz])); +VAL[SZ] =0; -CH[U][C] = sz++; $             } $  -U =Ch[u][c]; -         } theVal[u] =v; -ms[string(s)] =v;Wuyi     } the  -     //count the occurrences of each string Wu     voidCountintj) -     { About         if(j) $         { -cnt[val[j]]++; - count (Last[j]); -         } A     } +  the     //find a template in T -     voidFindChar*T) $     { the         intj =0, n =strlen (T); the          for(inti =0; I < n; i++) the         { the             intc =idx (t[i]); -              while(J &&!ch[j][c]) j =F[j]; inj =Ch[j][c]; the             if(Val[j]) count (j); the             Else if(Last[j]) count (Last[j]); About         } the     } the  the     //calculate a mismatch function +     voidGetfail () -     { thequeue<int>Q;Bayif[0] =0; the          for(inti =0; i < sigma_size; i++) the         { -             intU = ch[0][i]; -             if(u) {last[u] =0; F[u] =0; Q.push (u);} the         } the          while(!q.empty ()) the         { the             intR =Q.front (); Q.pop (); -              for(intc =0; c < sigma_size; C++) the             { the                 intU =Ch[r][c]; the                 if(!u)Continue;94 q.push (u); the                 intv =F[r]; the                  while(v &&!ch[v][c]) v =F[v]; theF[u] =Ch[v][c];98Last[u] = Val[f[u]]?F[u]: last[f[u]; About             } -         }101     }102 }ac;103 104 Const intMAXN =1000000+Ten; the CharT[MAXN], p[ the][ the];106 107 intMain ()108 {109     //freopen ("In.txt", "R", stdin); the 111     intN; the      while(SCANF ("%d", &n) = =1&&N)113     { the ac.init (); the          for(inti =1; I <= N; i++) {scanf ("%s", P[i]); Ac.insert (P[i], i); } thescanf"%s", T);117 Ac.getfail ();118 Ac.find (T);119         intM =-1; -          for(inti =1; I <= N; i++)if(Ac.cnt[i] > M) M =Ac.cnt[i];121printf"%d\n", M);122          for(inti =1; I <= N; i++)123             if(ac.cnt[ms[string(P[i])] ==M)124printf"%s\n", P[i]); the     }126 127     return 0; -}
code June

LA 4670 (AC automaton template problem) dominating Patterns

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.