hdu 2222 keywords search (AC自动机 模板)

本文详细介绍了AC自动机的实现过程,包括构建AC自动机、插入关键字、建立fail指针等核心步骤,并通过一个具体的编程实例展示了如何进行模式匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

我日,写错好多小细节,还差得远呢。。。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 500010
char str[1000010],keyword[51];
int head,tail;

typedef struct node{
    struct node*fail;
    struct node*next[26];
    bool exist;
    int count;
}*trie,node;
trie root;
trie q[N];

void creat(trie &p){
    p=(trie)malloc(sizeof(node));
    for(int i=0;i<26;i++)
        p->next[i]=NULL;
    p->exist=false;
    p->count=0;
    p->fail=NULL;             /*原来一切的根源在于root的fail没有初始化,日*/
}

void insert(trie p,char s[]){
    int k=0 ;
    while(s[k]!='\0'){
        if(!p->next[s[k]-'a']){
            trie q;
            creat(q);
            p->next[s[k]-'a']=q;
        }
        p=p->next[s[k]-'a'];
        k++;
    }
    p->count++;
}

void build_ac(){                         /*建立fail指针,BFS*/
    q[tail++]=root;
    while(head!=tail){
        node*p=q[head++];                  /*出队*/
        node*temp=NULL;
        for(int i=0;i<26;i++){
            if(p->next[i]!=NULL){
                if(p==root)
                    p->next[i]->fail=root;   /*root下面的26的fail必指向root*/
                else{
                    temp=p->fail;
                    while(temp!=NULL){
                        if(temp->next[i]!=NULL){
                            p->next[i]->fail=temp->next[i];
                            break;               /*找到了就break*/
                        }
                        temp=temp->fail;/*如果它的fail指针下的26个都没有的话,就指向fail指针的fail再找一次,直到退到root就也能退出来了*/
                    }
                    if(temp==NULL)
                        p->next[i]->fail=root;
                }
                q[tail++]=p->next[i];    /*入队*/
            }
        }
    }
}

int query(trie p){
    int index,len,result;
    result=0;
    len=strlen(str);
    for(int i=0;i<len;i++){                    /*写成了26,找了好久*/
        index=str[i]-'a';
        while(p->next[index]==NULL&&p!=root)   /*预测一下,下面没有了就赶紧跳到fail去。(root跳个屁啊)*/
             p=p->fail;
        p=p->next[index];
        if(p==NULL)
            p=root;
        trie temp=p;
        while(temp!=root&&temp->count!=-1){     /*通过fail指针看一下到目前为止的后缀串有没有单词*/
            result+=temp->count;
            temp->count=-1;
            temp=temp->fail;
        }
    }
    return result;
}
int main(){
    int ncase,n;
    scanf("%d",&ncase);
    while(ncase--){
        head=tail=0;
        creat(root);
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s",keyword);
            insert(root,keyword);
        }
        build_ac();
        scanf("%s",str);
        printf("%d\n",query(root));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值