【HDU2222】Keywords Search(AC自动机)

记录一个菜逼的成长。。

题目链接
题目大意:
给你n个字符串,最后给你一个字符串,问最后的字符串里包含前面n个里面的几种字符串

AC自动机入门。简单的说就是树上的KMP。
kuangbin的模板

失败指针的构造:
构造失败指针的过程概括起来就一句话:设这个节点上的字母为C,沿着他父亲的失败指针走,直到走到一个节点,他的儿子中也有字母为C的节点。然后把当前节点的失败指针指向那个字母也为C的儿子。如果一直走到了root都没找到,那就把失败指针指向root。具体操作起来只需要:先把root加入队列,这以后我们每处理一个点,就把它的所有儿子加入队列,队列为空。
不过在这个模板里,next里不存在的都指向了root,可以省略了很多过程。

#include <bits/stdc++.h>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
typedef long long LL;
const int INF = 0x3f3f3f3f;
struct Trie
{
    int next[500010][26],fail[500010],cnt[500010];
    int root,L;
    int newnode()
    {
        for(int i = 0;i < 26;i++)
            next[L][i] = -1;
        cnt[L++] = 0;
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void Insert(char buf[])
    {
        int len = strlen(buf);
        int now = root;
        for(int i = 0;i < len;i++)
        {
            if(next[now][buf[i]-'a'] == -1)
                next[now][buf[i]-'a'] = newnode();
            now = next[now][buf[i]-'a'];
        }
        cnt[now]++;
    }
    void build()
    {
        queue<int>Q;
        fail[root] = root;
        for(int i = 0;i < 26;i++)
            if(next[root][i] == -1)
                next[root][i] = root;
            else
            {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        while( !Q.empty() )
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0;i < 26;i++)
                if(next[now][i] == -1)
                    next[now][i] = next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
    }
    int query(char buf[])
    {
        int len = strlen(buf);
        int now = root;
        int res = 0;
        for(int i = 0;i < len;i++)
        {
            now = next[now][buf[i]-'a'];
            int temp = now;
            //这一步是因为一个单词的后缀可能是其他单词的前缀
            while( temp != root )
            {
                res += cnt[temp];
                cnt[temp] = 0;//防止重复计算(比如AAAA里包含3个AA,但这题只计算一个),但是要计算个数而不是种数时,这行不要。
                temp = fail[temp];
            }
        }
        return res;
    }
};
const int maxn = 1000000 + 10;
char str[maxn];
Trie ac;
int main()
{
    int T;scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        ac.init();
        for( int i = 0; i < n; i++ ){
            scanf("%s",str);
            ac.Insert(str);
        }
        ac.build();
        scanf("%s",str);
        int ans = ac.query(str);
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值