HDU 2222 AC自动机模板题

这道题真的是不想说什么了,数组没开够tmd报TLE,艹,白浪费老子一个小时的大好时光_________________________________
这道题建出ac自动机之后顺着next指针跳(代码中用fail直接代替了),每个点都加上一个标记只能算一次就完事了QAQ

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
struct Trie
{
    Trie *son[26],*fail;
    bool match;
    int sum;
    Trie()
    {
        memset(son,0,sizeof(son));
        fail=NULL;
        match=sum=0;
    }
}*root=new Trie();
void my_insert(char *s)
{
    Trie *o=root;
    while(*s)
    {
        int x=(*s)-'a';
        if(!o->son[x]) o->son[x]=new Trie();
        o=o->son[x];
        s++;
    }
    o->sum++;
}

void bfs()
{
    static Trie* dui[1200000];
    int top=1,my_final=1;
    for(int i=0;i<26;i++)
    {
        if(root->son[i])
        {
            dui[my_final++]=root->son[i];
            root->son[i]->fail=root;
        }
        else root->son[i]=root;
    }
    while(top<my_final)
    {
        Trie *o=dui[top];
        for(int i=0;i<26;i++)
        {
            if(o->son[i])
            {
                dui[my_final++]=o->son[i];
                o->son[i]->fail=o->fail->son[i];
            }
            else o->son[i]=o->fail->son[i];
        }
        top++;
        Trie *mid=o->fail;
        while(mid!=root && !mid->sum) mid=mid->fail;
        o->fail=mid;
    }
}
int find_ans(char *s)
{
    Trie *o=root;
    int ans=0;
    while(*s)
    {
        o=o->son[(*s)-'a'];
        Trie *t=o;
        while(t!=root && !t->match)
        {
            t->match=true;
            ans+=t->sum;
            t=t->fail;
        }
        s++;
    }
    return ans;
}
char s[1000101];
int main()
{
    int hahaha;
    scanf("%d",&hahaha);
    for(int h=1;h<=hahaha;h++)
    {
        int n;
        scanf("%d",&n);
        root=new Trie();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",s);
            my_insert(s);
        }
        bfs();
        scanf("%s",s);
        printf("%d\n",find_ans(s));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值