UVA 11019 AC自动机

点击打开链接

题意:给一个n*m的文本矩阵,然后给一个x*y的矩阵,问文本矩阵中有多少个x*y的矩阵

思路:先把x*y的矩阵构造AC自动机,然后一行一行的匹配n*m的矩阵,每次匹配成功,则以这行成功的开头应该是哪个位置,然后将位置的值+1,最后统计所有的位置的值等于x的所有的点就是成功的匹配点

#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1010;
const int N=26;
vector<int>G[maxn];
struct node{
    node *fail;
    node *next[N];
    int num;
    node(){
        fail=NULL;
        num=0;
        memset(next,NULL,sizeof(next));
    }
}*q[maxn*maxn];
char str1[maxn],str2[maxn][maxn];
int head,tail,cnt[maxn][maxn];
void insert_Trie(char *str,node *root,int iddd){
    node *p=root;
    int i=0;
    while(str[i]){
        int id=str[i]-'a';
        if(p->next[id]==NULL) p->next[id]=new node();
        p=p->next[id];i++;
    }
    if(p->num!=0) G[p->num].push_back(iddd);
    else{
        p->num=iddd+1;
        G[p->num].push_back(iddd);
    }
}
void build_ac(node *root){
    root->fail=NULL;
    q[head++]=root;
    while(head!=tail){
        node *temp=q[tail++];
        node *p=NULL;
        for(int i=0;i<N;i++){
            if(temp->next[i]!=NULL){
                if(temp==root) temp->next[i]->fail=root;
                else{
                    p=temp->fail;
                    while(p!=NULL){
                        if(p->next[i]!=NULL){
                            temp->next[i]->fail=p->next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL) temp->next[i]->fail=root;
                }
                q[head++]=temp->next[i];
            }
        }
    }
}
void query(char *str,node *root,int c){
    int i=0;
    node *p=root;
    while(str[i]){
        int id=str[i]-'a';
        while(p->next[id]==NULL&&p!=root) p=p->fail;
        p=p->next[id];
        p=(p==NULL)?root:p;
        node *temp=p;
        while(temp!=root&&temp->num!=0){
            for(unsigned int ppp=0;ppp<G[temp->num].size();ppp++) if(c-G[temp->num][ppp]>=0) cnt[c-G[temp->num][ppp]][i]++;
            temp=temp->fail;
        }
        i++;
    }
}
int main(){
    int T,n,x,y,m;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<maxn;i++) G[i].clear();
        memset(cnt,0,sizeof(cnt));
        node *root=new node();
        head=tail=0;
        for(int i=0;i<n;i++) scanf("%s",str2[i]);
        scanf("%d%d",&x,&y);
        for(int i=0;i<x;i++) scanf("%s",str1),insert_Trie(str1,root,i);
        build_ac(root);
        for(int i=0;i<n;i++) query(str2[i],root,i);
        int sum=0;
        for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(cnt[i][j]==x) sum++;
        printf("%d\n",sum);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值