http://acm.hust.edu.cn/vjudge/problem/33057
Test instructions: Find out how many times a two-dimensional template string p appears in a two-dimensional text string T.
Exercises
Split the template string p for each line, build an AC automaton.
Splits each line of the text string T, matches p in the automaton, Ct[i][j] indicates how many rows correspond to P with a point (I,J) as the upper-left corner, and a large rectangle such as p.
The i,j of the last ct[i][j]==p line is a matching point, ans++.
Note: 1. The leaves of the trie I used a dynamic array to maintain an array that indicates that this line is the first line of P, but timed out, then looked at the LRJ code, changed to a nt[i] to represent the next line of repeated rows, a.
2. Notice whether I,J is greater than 0 when Ct[i][j] Riga.
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <queue>6#include <vector>7 using namespacestd;8 9 intN,m,num,ans;Ten intct[1100][1100],nt[ the]; One Chars[1100][1100],ss[1100][1100]; A structnode{ - intson[ -]; - intfail,ed; the}a[ the* the]; -queue<int>Q; - - voidClearintx) + { -A[x].fail=a[x].ed=0; +memset (A[x].son,0,sizeof(A[x].son)); A } at - voidTrieChar*c,intID) - { - intL=strlen (c); - intx=0; - for(intI=0; i<l;i++) in { - intt=c[i]-'a'+1; to if(!A[x].son[t]) + { -num++; the Clear (num); *a[x].son[t]=num; $ }Panax Notoginsengx=A[x].son[t]; - } the if(!a[x].ed) a[x].ed=ID; + } A the voidBuildac () + { - while(!q.empty ()) Q.pop (); $ for(intI=1; i<= -; i++) $ if(a[0].son[i]) Q.push (a[0].son[i]); - while(!q.empty ()) - { the intx=Q.front (); Q.pop (); - intFail=A[x].fail;Wuyi for(intI=1; i<= -; i++) the { - inty=A[x].son[i]; Wu if(y) - { AboutA[y].fail=A[fail].son[i]; $ Q.push (y); - } - Elsea[x].son[i]=A[fail].son[i]; - } A } + } the - voidFindChar*c,intID) $ { the intL=strlen (c); the intx=0; the for(intI=0; i<l;i++) the { - intt=c[i]-'a'+1; in if(!a[x].son[t]) x=0; thex=A[x].son[t]; the intX0=id,y0= (i+1)-m+1; About intp=A[x].ed; the while(P) the { the intxx=x0-p+1, yy=y0; + if(xx>=1&& yy>=1)//Attention!! No, wa!!. - { thect[xx][yy]++;Bayi if(ct[xx][yy]>=n) ans++; the } thep=Nt[p]; - } - } the } the the intMain () the { -Freopen ("a.in","R", stdin); theFreopen ("a.out","W", stdout); the intT; thescanf"%d",&T);94 while(t--) the { thenum=ans=0; theClear0);98memset (CT,0,sizeof(CT)); Aboutmemset (NT,0,sizeof(NT));//Use the NT Array (next) to optimize it, or the tle. - intx, y;101scanf"%d%d",&x,&y);102 for(intI=1; i<=x;i++)103 {104scanf"%s", Ss[i]); the }106scanf"%d%d",&n,&m);107 for(intI=1; i<=n;i++)108 {109scanf"%s", S[i]); the trie (s[i],i);111 } the for(intI=1; i<=n;i++)113 for(intj=i+1; j<=n;j++) the { the if(strcmp (s[i],s[j]) = =0) {nt[i]=j; Break;} the }117 Buildac ();118 for(intI=1; i<=x;i++) find (ss[i],i);119printf"%d\n", ans); - }121 return 0;122}
"Uva11019-matrix Matcher" ac automata + optimization + recording