AC自动机 ( 动态建树模板 )——Keywords Search ( HDU 2222 )

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

  • 分析:
    给出n个单词建树,然后给出一个字符串,搜索字符串里出现了多少个单词。直接套用AC自动机模板就可以。

  • AC自动机模板(含解析):

const int k = 26;
const int MAXN = 500100;
struct Node
{
    Node* ch[k], *fail;
    int match;
    void clear()
    {
        memset(this, 0, sizeof(Node));
    }
};
Node * que[MAXN];
struct ACAutomaton
{
    Node nodes[MAXN],  *root,  *superRoot, *cur; //全局变量
    Node * newNode()  //从内存池中初始化一个结点
    {
        cur -> clear();
        return cur++;
    }
    void clear()  //清空整个字典树
    {
        cur = nodes;
        superRoot = newNode();
        root = newNode();
        root -> fail = superRoot;
        for(int i=0;i<k;i++)      //superRoot为虚拟的超级根结点,所有孩子均指向实际的根结点,减少建立自动机的代码量
            superRoot -> ch[i] = root;
        superRoot->match = -1;
    }
    void insert(char *s)//插入每一个字符,match++
    {
        Node * t = root;
        for(;*s;s++)
        {
            int x = *s - 'a';
            if(t -> ch[x] == NULL)
                t -> ch[x] = newNode();
            t = t -> ch[x];
        }
        t -> match++;
    }
    void build()            //使用自动机前,要先生成失配指针
    {
        int p=0, q =0;
        que[q++] = root;
        while(p!=q)         //BFS求失配指针【非常关键的一步,可以好好理解】
        {
            Node*t = que[p++];
            for(int i=0;i<k;i++)
            {
                if(t->ch[i])
                {
                    t -> ch[i] -> fail = t->fail ->ch[i];
                    que[q++] = t->ch[i];
                }else
                    t->ch[i] = t -> fail ->ch[i];

            }
        }
    }

    int run(char *s)            //计算s中模式串出现的次数
    {
        int ans = 0;
        Node * t = root;
        for(; *s ; s++)
        {
            int x = *s - 'a';
            t = t->ch[x];       //记住 t 每次迭代的值!!!便于理解
            for(Node*u = t; u->match != -1;u = u->fail)
            {
                ans += u ->match;
                u -> match = -1;   //避免重复计算
            }
        }
        return ans;
    }

};
  • AC代码:
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))

const int k = 26;
const int MAXN = 500100;
struct Node
{
    Node* ch[k], *fail;
    int match;
    void clear()
    {
        memset(this, 0, sizeof(Node));
    }
};
Node * que[MAXN];
struct ACAutomaton
{
    Node nodes[MAXN],  *root,  *superRoot, *cur; //全局变量
    Node * newNode()  //从内存池中初始化一个结点
    {
        cur -> clear();
        return cur++;
    }
    void clear()  //清空整个字典树
    {
        cur = nodes;
        superRoot = newNode();
        root = newNode();
        root -> fail = superRoot;
        for(int i=0;i<k;i++)      //superRoot为虚拟的超级根结点,所有孩子均指向实际的根结点,减少建立自动机的代码量
            superRoot -> ch[i] = root;
        superRoot->match = -1;
    }
    void insert(char *s)
    {
        Node * t = root;
        for(;*s;s++)
        {
            int x = *s - 'a';
            if(t -> ch[x] == NULL)
                t -> ch[x] = newNode();
            t = t -> ch[x];
        }
        t -> match++;
    }
    void build()            //使用自动机前,要先生成失配指针
    {
        int p=0, q =0;
        que[q++] = root;
        while(p!=q)         //BFS求失配指针
        {
            Node*t = que[p++];
            for(int i=0;i<k;i++)
            {
                if(t->ch[i])
                {
                    t -> ch[i] -> fail = t->fail ->ch[i];
                    que[q++] = t->ch[i];
                }else
                    t->ch[i] = t -> fail ->ch[i];

            }
        }
    }

    int run(char *s)            //在自动机上与匹配串s进行匹配
    {
        int ans = 0;
        Node * t = root;
        for(; *s ; s++)
        {
            int x = *s - 'a';
            t = t->ch[x];
            for(Node*u = t; u->match != -1;u = u->fail)
            {
                ans += u ->match;
                u -> match = -1;
            }
        }
        return ans;
    }

};

int n;
ACAutomaton j;
char s[1000100];
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        j.clear();
        while(n--)
        {
            scanf("%s", s);
            j.insert(s);
        }
        j.build();
        scanf("%s", s);
        printf("%d\n", j.run(s));

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值