AC自动机 LA4670

这题调了好久:

问题1:Trie图的节点数给小了,应该是150*70,我给的是150*26,少脑子了;

问题2:就是数组开小了,老是提交超时,导致开始怀疑算法的复杂度有问题;

另外,LA上这题的数据非常弱,非常非常弱。


AC自动机的本质:先把模板串建成Trie树,然后根据KMP的思想用BFS建Trie图,说穿了就是维护两个数组,f[u]往回找,last[u]指向其他较短模板串的节点编号,其实并不复杂,关键是理解清楚递推式。

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;
#define SUM 155
#define ML 75
#define N 11000 //155*26
#define SIG 26
#define MAXL 1000005

char p[SUM][ML], text[MAXL];
int num[SUM], cnt[SUM];
int n, T;

struct AC_auto {
    int ch[N][SIG];
    int val[N], f[N], last[N];
    int sz;

    void init() {
        sz=1;
        memset(ch[0],0,sizeof(ch[0]));
        memset(val,0,sizeof(val));
        //memset(f,0,sizeof(f));
       // memset(last,0,sizeof(last));
    }

    void Insert(char *s,int v) {
        int u=0, n=strlen(s);
        for (int i=0;i<n;i++) {
            int c= s[i]-'a';
            if (!ch[u][c]) {
                memset(ch[sz],0,sizeof(ch[sz]));
                //val[sz]=0;
                ch[u][c]=sz++;
            }
            u= ch[u][c];
        }
        val[u]=v;
        // correct
        num[v]=u;
    }

    void getFail(){
        queue<int> q;
        f[0]=0;
        //init
        for (int c=0;c<SIG;c++) {
            int u=ch[0][c];
            if (u) {
                f[u]=0;
                q.push(u);
                last[u]=0;
            }
        }
        //bfs
        while (!q.empty()) {
            int r=q.front();
            for (int c=0;c<SIG;c++) {
                int u=ch[r][c];
                if (!u) {
                    //key
                    ch[r][c]= ch[f[r]][c];
                    continue;
                }
                q.push(u);
                int v=f[r];
                while (v && !ch[v][c]) v=f[v];
                f[u]= ch[v][c];
                last[u] = val[f[u]] ? f[u] : last[f[u]];
            }
            q.pop();
        }
    }

    void Find(char* T){
        int n = strlen(T);
        int j = 0;
        for (int i=0;i<n;i++) {
            int c=T[i]-'a';
            while (j && !ch[j][c]) j=f[j];
            j= ch[j][c];
            //correct
            int temp = j;
            if (val[temp]) {
                while (temp) {
                    cnt[val[temp]]++;
                    temp=last[temp];
                }
            }
            else if (last[temp]) {
                temp=last[temp];
                while (temp) {
                    cnt[val[temp]]++;
                    temp=last[temp];
                }
            }
        }
    }
};

AC_auto ac;

int main()
{
    freopen("1.in","r",stdin);

    while (scanf("%d",&n)!=EOF && n) {
        ac.init();
        memset(cnt,0,sizeof(cnt));
        memset(num,0,sizeof(num));
        for (int i=1;i<=n;i++){
            scanf("%s",p[i]);
            ac.Insert(p[i],i);
        }
        ac.getFail();
        scanf("%s",text);
        ac.Find(text);
        int best= -1;
        for (int i=1;i<=n;i++){
            if (cnt[i] >best) best= cnt[i];
        }
        printf("%d\n",best);
        for (int i=1;i<=n;i++)
            if (cnt[ac.val[num[i]]]==best)
                printf("%s\n",p[i]);
    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值