UVA 11019(Matrix Matcher-vector從迭代器中取值,AC自動機匹配字元矩陣),vector迭代器

來源:互聯網
上載者:User

UVA 11019(Matrix Matcher-vector從迭代器中取值,AC自動機匹配字元矩陣),vector迭代器

Problem H
Matrix Matcher
Input: 
Standard Input

Output: Standard Output

 

Given an N * M matrix, your task is to find the number of occurences of 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 two integers N and M (N, M ≤ 1000). The next N lines contain M characters each.

 

The next line contains two integers X and Y (X, Y ≤ 100). The next X lines contain Y characters each. 

 

Output

For each case, output a single integer in its own line, the number of occurrences.

 

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 please make sure that you use a fast IO function (eg.scanf()) to read input.

 

AC自動機,把字串P扔入AC自動機,然後去匹配T矩陣每行字元,

每次成功匹配,就把以當前行匹配算出來的矩陣的右上方cnt++,被+x次說明匹配矩陣成功。

注意vector中的元素用(*it)

PS:由於P中單詞長度一致,故不用在print迴圈last

PS2:有可能同一行出現2次覆蓋v,因此要用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 (26)typedef long 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-b)/F*F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}int cnt[MAXN][MAXN]; //右上方 class Aho_Corasick_Automata{public:int ch[MAXNode][Sigma_size],siz; vector<int> v[MAXNode];// AC自動機 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 表示單詞末尾,       PS:lrj叫它單詞節點  {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) //列印全串中所有以j為末尾的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;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.