AC自动机模板

用  HDU2222   这题作为模板
#include <bits/stdc++.h>

using namespace std;

struct ACautoman{
    int endsum;
    ACautoman *next[26];
    ACautoman *fail;
    ACautoman (){
        endsum = 0;
        fail = NULL;
        memset(next,NULL,sizeof(next));
    }
}*root;

void CreatTrie(char *str){
    ACautoman *p = root;
    int len = strlen(str);
    for(int i=0; i<len; i++){
        if(!p -> next[ str[i]-'a' ]) p -> next[ str[i]-'a' ] = new ACautoman;
        p = p -> next[ str[i]-'a' ];
    }
    ++(p -> endsum);
}

void CreatFail(){
    queue <ACautoman *> Q;
    Q.push(root);
    while(!Q.empty()){
        ACautoman *p = Q.front();
        Q.pop();
        for(int i=0; i<26; i++){
            if(p -> next[i]){
                if(p == root) p -> next[i] -> fail = root;
                else{
                    ACautoman *now = p;
                    while(now -> fail){
                        if(now -> fail -> next[i]){
                            p -> next[i] -> fail = now -> fail -> next[i];
                            break;
                        }
                        now = now -> fail;
                    }
                    if(!now -> fail) p -> next[i] -> fail = root;
                }
                Q.push(p -> next[i]);
            }
        }
    }
}

int Search(char *str){
    int ans = 0;
    int len = strlen(str);
    ACautoman *p = root;
    for(int i=0; i<len; i++){
        while(p != root && !p -> next[ str[i]-'a' ]) p = p -> fail;
        if( p -> next[ str[i]-'a' ] ){
            p = p -> next[ str[i]-'a' ];
            ACautoman *now = p;
            while(now != root ){
                if(now -> endsum){
                    ans += now -> endsum;
                    now -> endsum = 0;
                }
                now = now -> fail;
            }
        }
    }
    return ans;
}

const int maxn = 1000005;
char text[maxn];

int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--){
        root = new ACautoman;
        int N;
        cin>>N;
        char word[55];
        for(int i=0; i<N; i++){
            cin>>word;
            CreatTrie(word);
        }
        cin>>text;
        CreatFail();
        cout<< Search(text) <<endl;
    }
    return 0;
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值