hdu 2222 AC自动机模板题

首先学KMP   推荐《算法导论》以及本人的KMP博文 http://blog.csdn.net/u011026968/article/details/10382659

在学Trie  这个其实不难,随意找点资料就行

然后开始学AC自动机  http://www.cppblog.com/mythit/archive/2014/03/09/80633.html#206110  这个博文的讲解很好 但是看评论似乎有bug?我自己找的我队友写的模板,然后做点改动作为我自己的模板了

上模版:

/*************************************************/
//AC 自动机  by Pilgrim
//
//MAXLEN 模式串的长度
//str 模式串(待匹配的)
//keyword 待输入的单词
//cnt是否为该单词的最后一个节点,Insert的时候,
//当单词插入完成,其最后一个节点的cnt=1
//root的fail为NULL
//
// 初始化
//root=cur=Trie;
//head = tail = 0;
//root->clr();
//另外在Insert的时候  创建节点的时候也是要clr()的
/*************************************************/

#define MAXLEN 1000010
#define MAXTRIE 500010
#define WORDLEN 51
#define KIND 26

char str[MAXLEN],keyword[WORDLEN];

struct Node{
    Node *fail;
    Node *next[KIND];   /*next数组里存的是当前节点的孩子*/
    int cnt;
    void clr()
    {
        fail = NULL;
        cnt = 0;
        memset(next,0,sizeof(next)*KIND);
    }
}Trie[MAXTRIE],*q[MAXTRIE],*root,*cur;  /*看最开头的注释*/
int head,tail;/*队列首尾 初始化head = tail = 0*/

void Insert(char s[])   /*向Tries 插入单词*/
{
    int idx,i,n=strlen(s);
    Node *p=root;
    for(int i=0;i<n;i++)
    {
        idx = s[i]-'a';
        if(p->next[idx]==NULL){
            p->next[idx]=++cur;
            p->next[idx]->clr();
        }
        p=p->next[idx];
    }
    p->cnt++;   /*插入完成*/
}

void Build_AC()
{
    Node *p,*tmp;
    root->fail=NULL;
    q[tail++]=root;
    while(head!=tail)
    {
        p=q[head++];
        for(int i=0;i<KIND;i++)
        {
            if(p->next[i])
            {
                q[tail++]=p->next[i];
                if(p == root)
                {
                    p->next[i]->fail = root;
                }
                else
                {
                    tmp=p->fail;
                    while(tmp!=NULL)
                    {
                        if(tmp->next[i])    /*tmp->next[i] p->next[i]  i都表示'a'+i故如果tmp->next[i]!=NULL,说明以前出现过'a'+i*/
                        {
                            p->next[i]->fail=tmp->next[i];
                            break;
                        }
                        tmp=tmp->fail;
                    }
                    if(tmp == NULL)
                        p->next[i]->fail = root;
                }
            }
        }
    }
}

//int Query(char str[])
int Query()
{
    int ans=0,n=strlen(str),idx;
    Node *tmp,*p=root;
    for(int i=0;i<n;i++)
    {
        idx=str[i]-'a';
        while(p->next[idx]==NULL && p!=root) //跳转失败指针
            p=p->fail;
        p=p->next[idx];
        if(p==NULL)
            p=root;
        tmp = p;	//p不动,tmp计算后缀串
        while(tmp!=root && tmp->cnt!=-1)/*理解的还有些问题*/
        {
            ans+=tmp->cnt;
            tmp->cnt=-1;
            tmp=tmp->fail;//指针移向下个字符继续匹配
        }
    }
    return ans;
}

void Init()
{
    cur = root = Trie;
    root->clr();
    head = tail = 0;
}


然后hdu 2222直接套模版


/*******************************************************/
//hdu 2222 AC自动机 by Pilgrim
/******************************************************/

#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

#define MAXLEN 1000010
#define MAXTRIE 500010
#define WORDLEN 51
#define KIND 26

char str[MAXLEN],keyword[WORDLEN];

struct Node{
    Node *fail;
    Node *next[KIND];   /*next数组里存的是当前节点的孩子*/
    int cnt;
    void clr()
    {
        fail = NULL;
        cnt = 0;
        memset(next,0,sizeof(next)*KIND);
    }
}Trie[MAXTRIE],*q[MAXTRIE],*root,*cur;  /*看最开头的注释*/
int head,tail;/*队列首尾 初始化head = tail = 0*/

void Insert(char s[])   /*向Tries 插入单词*/
{
    int idx,i,n=strlen(s);
    Node *p=root;
    for(int i=0;i<n;i++)
    {
        idx = s[i]-'a';
        if(p->next[idx]==NULL){
            p->next[idx]=++cur;
            p->next[idx]->clr();
        }
        p=p->next[idx];
    }
    p->cnt++;   /*插入完成*/
}

void Build_AC()
{
    Node *p,*tmp;
    root->fail=NULL;
    q[tail++]=root;
    while(head!=tail)
    {
        p=q[head++];
        for(int i=0;i<KIND;i++)
        {
            if(p->next[i])
            {
                q[tail++]=p->next[i];
                if(p == root)
                {
                    p->next[i]->fail = root;
                }
                else
                {
                    tmp=p->fail;
                    while(tmp!=NULL)
                    {
                        if(tmp->next[i])    /*tmp->next[i] p->next[i]  i都表示'a'+i故如果tmp->next[i]!=NULL,说明以前出现过'a'+i*/
                        {
                            p->next[i]->fail=tmp->next[i];
                            break;
                        }
                        tmp=tmp->fail;
                    }
                    if(tmp == NULL)
                        p->next[i]->fail = root;
                }
            }
        }
    }
}

//int Query(char str[])
int Query()
{
    int ans=0,n=strlen(str),idx;
    Node *tmp,*p=root;
    for(int i=0;i<n;i++)
    {
        idx=str[i]-'a';
        while(p->next[idx]==NULL && p!=root) //跳转失败指针
            p=p->fail;
        p=p->next[idx];
        if(p==NULL)
            p=root;
        tmp = p;	//p不动,tmp计算后缀串
        while(tmp!=root && tmp->cnt!=-1)/*理解的还有些问题*/
        {
            ans+=tmp->cnt;
            tmp->cnt=-1;
            tmp=tmp->fail;//指针移向下个字符继续匹配
        }
    }
    return ans;
}

void Init()
{
    cur = root = Trie;
    root->clr();
    head = tail = 0;
}

int main()
{
    int ncase,n;
    scanf("%d",&ncase);
    while(ncase--)
    {
        Init();
        scanf("%d",&n);

        while(n--)
        {
            scanf("%s",keyword);
            Insert(keyword);
        }
        Build_AC();
        scanf("%s",str);
        printf("%d\n",Query());
    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值