AC自动机的初步学习 hdu2222 AC自动机入门题

本来KMP和trie树就不太熟悉,但是就是想弄明白AC自动机是什么东东,所以就找了一些博客学习了一下,用hdu2222练了一下手。

推荐几篇很好的博客:

http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html

这篇博客绝对是良心之作,尤其是那些图片,可以很好地讲述AC自动机的失败指针和trie图构造

还有几篇不错的:

http://blog.csdn.net/mobius_strip/article/details/22549517

http://blog.csdn.net/niushuai666/article/details/7002823


对于做题的话,可以看kuangbin巨巨的专题,我的代码也是照着他的敲得。

http://www.cnblogs.com/kuangbin/p/3164106.html


如果基本明白了,就结合题目来练一下,hdu2222是基本题,推荐一篇注释详细的题解。

http://blog.sina.com.cn/s/blog_69c3f0410100tztt.html


代码:

#include<iostream>
#include<vector>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
const int N=10000*50+5;
struct Trie
{
    int next[N][26],fail[N],end[N];
    int rt,L;
    int newnode()
    {
        memset(next[L],-1,sizeof(next[L]));
        end[L++]=0;
        return L-1;
    }
    void init()
    {
        L=0;
        rt=newnode();
    }
    void insert(char *s)
    {
        int n=strlen(s);
        int p=rt;
        for(int i=0;i<n;i++){
            if(next[p][s[i]-'a']==-1)
                next[p][s[i]-'a']=newnode();
            p=next[p][s[i]-'a'];
        }
        end[p]++;
    }
    void build()
    {
        queue<int>Q;
        fail[rt]=rt;
        for(int i=0;i<26;i++){
            if(next[rt][i]==-1)next[rt][i]=rt;
            else fail[next[rt][i]]=rt,Q.push(next[rt][i]);
        }
        while(!Q.empty()){
            int p=Q.front();
            Q.pop();
            for(int i=0;i<26;i++){
                if(next[p][i]==-1)next[p][i]=next[fail[p]][i];
                else{
                    fail[next[p][i]]=next[fail[p]][i];
                    Q.push(next[p][i]);
                }
            }
        }
    }
    int query(char *s)
    {
        int n=strlen(s);
        int p=rt,ans=0;
        for(int i=0;i<n;i++){
            p=next[p][s[i]-'a'];
            int t=p;
            while(t!=rt){
                ans+=end[t];
                end[t]=0;
                t=fail[t];
            }
        }
        return ans;
    }
    void debug()
    {
        for(int i = 0;i < L;i++)
        {
            printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
            for(int j = 0;j < 26;j++)
                printf("%2d",next[i][j]);
            printf("]\n");
        }
    }
};
char str[1000010];
Trie ac;
int main()
{
    int T;
    int n;
    //freopen("f.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        ac.init();
        while(n--){
            scanf("%s",str);
            ac.insert(str);
        }
        ac.build();
       // ac.debug();
        scanf("%s",str);
        printf("%d\n",ac.query(str));
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值