HDU 2222 Keywords Search AC自动机模版

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222

题意:给你n个单词和一个字符串,问有多少个单词出现过

 

#include<bits/stdc++.h>
using namespace std;
const int N=500000+5;
struct p
{
    int next[N][26];
    int fail[N],cnt[N];
    int tot,root;

    int newnode()
    {
        for(int i=0;i<26;i++)
            next[tot][i]=-1;
        fail[tot]=-1;
        cnt[tot]=0;
        return tot++;
    }
    void init()
    {
        tot=0;
        root=newnode();
    }
    void add(char *s)
    {
        int now=root;
        int len=strlen(s);
        for(int i=0;i<len;i++)
        {
            if (next[now][s[i]-'a']==-1) next[now][s[i]-'a']=newnode();
            now=next[now][s[i]-'a'];
        }
        ++cnt[now];
    }
    void build()
    {
        queue<int> que;
        que.push(root);
        while(!que.empty())
        {
            int now=que.front();
            que.pop();
            for(int i=0;i<26;i++)
            {
                if (next[now][i]==-1)
                {
                    if (now==root) next[now][i]=root;
                    else next[now][i]=next[fail[now]][i];
                }
                else
                {
                    if (now==root) fail[next[now][i]]=root;
                    else fail[next[now][i]]=next[fail[now]][i];
                    que.push(next[now][i]);
                }
            }
        }
    }
    int match(char *s)
    {
        int now=root,ans=0;
        int len=strlen(s);
        for(int i=0;i<len;i++)
        {
            now=next[now][s[i]-'a'];
            for(int t=now;t!=root&&cnt[t]!=-1;t=fail[t])
            {
                ans+=cnt[t];
                cnt[t]=-1;
            }
        }
        return ans;
    }
}ac;
char s[1000005];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        ac.init();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",s);
            ac.add(s);
        }
        ac.build();
        scanf("%s",s);
        printf("%d\n",ac.match(s));
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/bk-201/p/8395476.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值