Problem H
Matrix Matcher
Input: standard input
Output: Standard Output
Given a N * M Matrix, your task is to find the number of occurences in an X * Y pattern.
Input
The first line contains a single integer t (t≤15), the number of test cases.
For each case, the first line contains the integers n and M (n, m≤1000). The next N lines contain M characters each.
The next line contains the integers x and Y (x, y≤100). The next X lines contain Y characters each.
Output
For each case, output A, the number of occurrences, and the own line.
Sample input Output for sample input
2 1 1 X 1 1 Y 3 3 Abc Bcd Cde 2 2 Bc Cd |
0 2
|
Problem Setter:rujia Liu, EPS
Special Thanks:wenbin Tang
Warming:the judge input file size is about 7 MB. So, make sure. A fast IO function (EG.SCANF ()) to read input.
ac automata, throw the string p into the AC automaton, and then match each line of the T-matrix character,
For each successful match, the top right corner of the matrix calculated with the current line match is cnt++, and the matching matrix is +x to be successful.
Note the elements used in vectors (*it)
PS: Because the word length in P is the same, it is not used in the print loop last
PS2: It is possible to overwrite V on the same line 2 times, so use a vector
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime> #include <queue> #include <vector>using namespace std; #define for (i,n) for (int i=1;i<=n;i++) #define FORK (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int. i=0;i<n;i++) #define ForD (i,n) for (int i=n;i;i--) #define REPD (I,n) for (int i=n;i>=0;i--) #define FORP (x) for (int p=pre[x];p, p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p= NEXT[P]) #define LSON (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)); #define Memi (a) memset (A,127,sizeof (a)), #define MEMI (a) memset (A,128,sizeof (a)), #define INF (2139062143) #define F (1000000007) # Define MAXT (15+10) #define MAXN (1000+10) #define MAXX (100+10) #define MAXNODE (1000000) #define SIGMA_SIZE (+) typedef lon G Long Ll;ll Mul (ll A,ll b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (lL A,LL b) {return (a-b+ (a)/f*f+f)%F;} void Upd (ll &a,ll b) {a= (a%f+b%f)%F;} int CNT[MAXN][MAXN]; upper right corner class Aho_corasick_automata{public:int ch[maxnode][sigma_size],siz; vector<int> v[MAXNode];//ac automaton int f[ Maxnode],last[maxnode]; Aho_corasick_automata (int _siz=0): siz (_siz) {MEM (ch) Rep (I,maxnode) v[i].clear (); Mem (f) Mem (last)}void mem (int _siz=0) {siz=_siz; MEM (CH) Rep (I,maxnode) v[i].clear (); Mem (f) Mem (last)}int idx (char c) {return C ' a ';} void Insert (char *s,int val=1)//val!=0 represents the end of the word, PS:LRJ calls it the word node {int U=0,n=strlen (s); Rep (i,n) {int c=idx (s[i]), if (!ch[u][c]) {++siz; MEM (Ch[siz]); ch[u][c]=siz;} U=CH[U][C];} V[u].push_back (val);} void Getfail () {queue<int> q; Rep (c,sigma_size) {int u=ch[0][c];if (u) q.push (u), last[u]=0;} while (!q.empty ()) {int R=q.front (); Q.pop (); R--c-->urep (c,sigma_size) {int u=ch[r][c];if (!u) {ch[r][c]=ch[f[r]][c]; continue;} q.push (U); f[u]=ch[f[r]][c]; Last[u]=v[f[u]].size ()? F[u]:last[f[u]];}}} void print (int j,int r,int c)//print all strings with the end of J as Str {for (Vector<int>::iterator It=v[j].begin (); It!=v[j].end (); it++) {int p_i= (*it), if (R (p_i-1) <0) continue; cnt[r- (p_i-1)] [C]++;}} void Find (char *s,int r) {int U=0,n=strlen (s); Rep (i,n) {int c=idx (s[i]), U=ch[u][c];if (V[u].size ()) print (u,r,i), else if (Last[u]) print (u,r,i);}} T;int N,m,x,y;char S[maxn][maxn];char s2[maxn];int Main () {//freopen ("uva11019.in", "R", stdin);//freopen (". Out", "w", stdout); int tt;scanf ("%d", &tt), while (tt--) {t.mem (); scanf ("%d%d", &n,&m); Rep (i,n) {scanf ("%s", S[i]);} scanf ("%d%d", &x,&y); for (i,x) {scanf ("%s", S2); T.insert (s2,i);} T.getfail (); MEM (CNT) REP (i,n) {char *str=s[i]; T.find (str,i);} int ans=0; Rep (I,n) {rep (j,m) ans+= (BOOL) (CNT[I][J]==X);} Cout<<ans<<endl;} return 0;}
UVA 11019 (Matrix matcher-vector values from iterators, ac automata match character matrices)