// hdu2222 // AC自动机初学

本文详细介绍了AC自动机的构建过程及其应用实例。通过具体的代码实现展示了如何进行字符串匹配,并提供了构建AC自动机的方法,包括节点结构定义、插入操作、构建失败指针等关键步骤。
// hdu2222 //
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
char k[55],s[1000005];
struct node
{
    int fail;
    int next[26];
    int cnt;
    void newnode()
    {
        cnt=0;
        fail=-1;
        for(int i=0;i<26;i++)
        {
            next[i]=-1;
        }
    }
}t[500005];
int root=0,tot=0;
void insert(char *str)
{
    int p=root;
    int len=strlen(str);
    for(int i=0;i<len;i++)
    {
        int id=str[i]-'a';
        if(t[p].next[id] == -1)
        {
            t[++tot].newnode();
            t[p].next[id] = tot;
        }
        p = t[p].next[id];
    }
    t[p].cnt++;
}
void build_ac()
{
    queue<int>q;
    q.push(root);
    while(!q.empty())
    {
        int p=q.front();
        q.pop();
        for(int i=0;i<26;i++)
        {
            if(t[p].next[i] != -

转载于:https://www.cnblogs.com/wikioibai/p/4471482.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值