hdu2222

AC自动机模板。首先定义一个功能很强大的结构体,里边包含有关的所有的应用,再对文章进行多模式匹配。

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string>
#include <memory.h>
using namespace std;

const int maxn = 500010;
char str[1000010];

struct ACAutomation
{
    int ch[maxn][26],val[maxn],last[maxn],fail[maxn],sz,root;
    int newnode(){
       memset(ch[sz],0,sizeof(ch[sz]));
       val[sz] = 0;
       return sz ++;
    }
    void init(){
       sz = 0;
       root = newnode();
    }
    void insrt(char *str){
       int len = strlen(str);
       int now = root;
       for(int i = 0;i < len;i ++){
        int &tmp = ch[now][str[i] - 'a'];
        if(!tmp)  tmp = newnode();
        now = tmp;
       }
       val[now] ++;
    }
    void getfail(){
       queue<int> q;
       fail[root] = root;
       for(int i = 0;i < 26;i ++){
        int u = ch[root][i];
        if(u){
            fail[u] = last[u] = 0;
            q.push(u);
        }
       }
       while(!q.empty()){
        int now = q.front();q.pop();
        for(int i = 0;i < 26;i ++){
            int u = ch[now][i];
            if(!u) ch[now][i] = ch[fail[now]][i];
            else{
                fail[u] = ch[fail[now]][i];
                last[u] = val[fail[u]] ? fail[u] : last[fail[u]];
                q.push(u);
            }
        }
       }
    }
       int query(char *str){
        int len = strlen(str);
        int now = root;
        int ret = 0;
        for(int i = 0;i < len;i++){
            now = ch[now][str[i]-'a'];
            int tmp = now;
            while(tmp != root && val[tmp]){
                ret += val[tmp];
                val[tmp] = 0;
                tmp = last[tmp];
            }
        }
        return ret;
    }

}cat;

int main()
{
    int T,n;scanf("%d",&T);
    while(T --){
        cat.init();
        scanf("%d",&n);
        for(int i = 0;i < n;i ++){
            scanf("%s",str);
            cat.insrt(str);
        }
        cat.getfail();
        scanf("%s",str);
        printf("%d\n",cat.query(str));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值