UVa 11019 (AC automaton Two-dimensional pattern string matching) Matrix Matcher

Source: Internet
Author: User

Just like in the book, if the line I of the pattern string p appears in column C of line R of the text string T, then cnt[r-i][c]++;

Another tricky problem is that there may be the same string in the pattern string, so use Repr[i] to record the position of the first occurrence of the I pattern string p[i]. If repr[i] = = I, indicating that the pattern string has not been repeated before, can be added to the automaton. If there are duplicates, organize the repeating patterns into a list and use next to connect them together.

So when you count CNT, the pattern string that matches is likely to be the matching line I, or next[i] line, Next[next[i]] line, and so on.

1#include <cstdio>2#include <cstring>3#include <queue>4 using namespacestd;5 6 intN, m, x, y, tr;7 Const intMaxx = -+Ten;8 Const intMAXN = ++Ten;9 Const intMaxnode =10000+Ten;Ten Const intSigma_size = -; One CharT[MAXN][MAXN], P[maxx][maxx]; A intCNT[MAXN][MAXN]; - intRepr[maxx]; - intNext[maxx]; the  - structAhocorasickautomata - { -     intCh[maxnode][sigma_size]; +     intF[maxnode]; -     intLast[maxnode]; +     intVal[maxnode]; A     intsz; at  -     voidInit () {sz =1; memset (ch[0],0,sizeof(ch[0])); } -  -InlineintIdxCharc) {returnC'a'; } -  -     voidInsertChar* S,intv) in     { -         intU =0, n =strlen (s); to          for(inti =0; I < n; i++) +         { -             intc =idx (s[i]); the             if(!Ch[u][c]) *             { $memset (Ch[sz],0,sizeof(Ch[sz]));Panax NotoginsengVAL[SZ] =0; -CH[U][C] = sz++; the             } +U =Ch[u][c]; A         } theVal[u] =v; +     } -  $     voidMatchintIintj) $     { -         intc = i-y +1; -         intPR = repr[val[j]-1]; the          while(PR >=0) -         {Wuyi             if(TR-PR >=0) cnt[tr-pr][c]++; thePR =NEXT[PR]; -         } Wu     } -  About     voidPrintintIintj) ${//match the column I in the text string to the word node J -         if(j) -         { - Match (I, j); A print (i, last[j]); +         } the     } -  $     voidFindChar*T) the     { the         intj =0, n =strlen (T); the          for(inti =0; I < n; i++) the         { -             intc =idx (t[i]); in              while(J &&!ch[j][c]) j =F[j]; thej =Ch[j][c]; the             if(Val[j]) print (I, j); About             Else if(Val[last[j]]) print (I, last[j]); the         } the     } the  +     voidGetfail () -     { thequeue<int>Q;Bayif[0] =0; the          for(intc =0; c < sigma_size; C++) the         { -             intU = ch[0][c]; -             if(u) {f[u] =0; Last[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 intMain () the {106     //freopen ("In.txt", "R", stdin);107 108     inttest;109scanf"%d", &test); the      while(test--)111     { thescanf"%d%d", &n, &m);113          for(inti =0; I < n; i++) scanf ("%s", T[i]); thescanf"%d%d", &x, &y); the ac.init (); the          for(inti =0; i < x; i++)117         {118Repr[i] =i;119Next[i] =-1; -scanf"%s", P[i]);121              for(intj =0; J < I; J + +)if(strcmp (P[i], p[j]) = =0)122             {123Repr[i] =J;124Next[i] =Next[j]; theNEXT[J] =i;126                  Break;127             } -             if(Repr[i] = i) Ac.insert (P[i], i+1);129         } the Ac.getfail ();131memset (CNT,0,sizeof(CNT)); the          for(tr =0; TR < n; tr++) Ac.find (T[tr]);133 134         intAns =0;135          for(inti =0; I < n; i++)136              for(intj =0; J < M; J + +)137                 if(Cnt[i][j] = = x) ans++;138printf"%d\n", ans);139     } $ 141     return 0;142}
code June

UVa 11019 (AC automaton Two-dimensional pattern string matching) Matrix Matcher

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.