HDU 2222 目标串中模式串数量(AC自动机)

题意:给定n(n≤50)个模式串(每个模式串长度不超过50)和1个目标串(长度不超过1000000),问目标串中包含多少个模式串。

思路:用模式串建立DFA,然后用目标串扫一遍即可。使用数组建立DFA,模板参考(http://www.cnblogs.com/kuangbin/p/3164106.html)。数组开500000一只TLE,减小到250000才能AC(但是链接中的数组开的就是500000呀。。。)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstdlib>
using namespace std;
#define N 250010
char s[1000005];
queue<int> q;
int t[N][26],fail[N],num[N];
int T,n,m,top,res;
void init(){
    int i;
    top = 1;
    res = 0;
    memset(fail, 0, sizeof(fail));
    memset(t, -1, sizeof(t));
    memset(num, 0, sizeof(num));
    for(i = 0;i<26;i++)
        t[0][i] = 1;
}

void insert(char* s){//建立trie树,
    int i,r = 1;
    for(i = 0;s[i];i++){
        if(t[r][s[i]-'a']==-1)
            t[r][s[i]-'a'] = ++top;
        r = t[r][s[i]-'a'];
    }
    num[r] ++;
}
void buildDFA(){//构造DFA
    int i,now;
    q.push(1);
    while(!q.empty()){
        now = q.front();
        q.pop();
        for(i = 0;i<26;i++){
            if(t[now][i] == -1)
                t[now][i] = t[fail[now]][i];
            else{
                fail[t[now][i]] = t[fail[now]][i];
                q.push(t[now][i]);
            }
        }
    }
}
void search(char* s){
    int i,j,r = 1;
    for(i = 0;s[i];i++){
        j = r = t[r][s[i]-'a'];
        while(j){
            res += num[j];
            num[j] = 0;
            j = fail[j];
        }
    }
}
int main(){
    scanf("%d",&T);
    while(T--){
        int i;
        init();
        scanf("%d",&n);
        for(i = 0;i<n;i++){
            scanf("%s",s);
            insert(s);
        }
        buildDFA();
        scanf("%s",s);
        search(s);
        printf("%d\n",res);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值