HDU 2222 Keywords Search

http://www.notonlysuccess.com/index.php/aho-corasick-automaton/    HH大牛博客

以下参照他的模板写的:数组模拟。

HDU-2222     Keywords Search          http://acm.hdu.edu.cn/showproblem.php?pid=2222

题意:

网络流上流传最广的AC自动机模板题,问你目标串中出现了几个模式串

如果一个结点是单词末尾的话out标记为true,在search的时候对于每个结点都向fail指针找,找到out为true的就将其标记为false,且ans++

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>

#define CL(arr, val)    memset(arr, val, sizeof(arr))

#define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)

#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d\n", x)
#define lowbit(x)   (x)&(-x)
#define Read()  freopen("din.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);


#define Nn 510007
#define Mc 26

using namespace std;

class Acautomaton
{
    private:

    int chd[Nn][Mc];
    int fail[Nn];
    int ID[Mc];
    int val[Nn];
    int Q[Nn];
    int sz;

    public:

    void Init()
    {
        fail[0] = 0;
        for (int i = 0; i < Mc; ++i)
        {
            ID[i] = i;
        }
    }
    void Reset()
    {
        CL(chd[0],0);
        sz = 1;
    }
    void insert(char *s,int key)
    {
        int p = 0;
        for (; *s; s++)
        {
            int k = ID[*s - 'a'];
            if (!chd[p][k])
            {
                CL(chd[sz],0);
                val[sz] = 0;
                chd[p][k] = sz++;
            }
            p = chd[p][k];
        }
        val[p] += key;
    }
    void Build()
    {
        int *s = Q ,*e = Q,i;
        for (i = 0; i < Mc; ++i)
        {
            if (chd[0][i])
            {
                *e++ = chd[0][i];
                fail[chd[0][i]] = 0;
            }
        }
        while (s != e)
        {
            int u = *s++;
            for (i = 0; i < Mc; ++i)
            {
                int &v = chd[u][i];
                if (v)
                {
                    *e++ = v;
                    fail[v] = chd[fail[u]][i];
                }
                else v = chd[fail[u]][i];
            }
        }
    }
    int solve(char *s)
    {
        int p = 0;
        int k,ans = 0;
        for (; *s; s++)
        {
            k = ID[*s - 'a'];
            while (!chd[p][k] && p != 0) p = fail[p];

            p = chd[p][k];

            int rt = p;
            while (rt != 0 && val[rt] != -1)
            {
                ans += val[rt];
                val[rt] = -1;
                rt = fail[rt];
            }
        }
        return ans;
    }
}ac;

char str[57],tr[1000007];
int n;

int main()
{
    int T;
    int i;
    scanf("%d",&T);
    while (T--)
    {
        ac.Reset(); ac.Init();
        scanf("%d",&n);
        for (i = 0; i < n; ++i)
        {
            scanf("%s",str);
            ac.insert(str,1);
        }
        ac.Build();
        scanf("%s",tr);
        printf("%d\n",ac.solve(tr));
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值