码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2222 Keywords Search(ac自动机)

时间:2016-11-19 15:54:16      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:fail   std   using   printf   自动   arch   root   string   原理   

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

题意:给你一系列子串,再给你一个主串问你主串一共有几个匹配子串

原来使用字典树写的但数据有点大TLE了,然后就开始学习ac自动机了,ac自动机就像是多串匹配的kmp原理也是类似的。

这题可以练一下手写ac自动机模版。

 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int M = 5e5 + 50;
int Next[M][26] , fail[M] , End[M] , root , L , cnt;
char str[60] , sl[M * 2];
int newnode() {
    for(int i = 0 ; i < 26 ; i++) {
        Next[L][i] = -1;
    }
    End[L++] = 0;
    return L - 1;
}
void init() {
    L = 0;
    root = newnode();
}
void build(char s[]) {
    int len = strlen(s);
    int now = root;
    for(int i = 0 ; i < len ; i++) {
        int id = s[i] - ‘a‘;
        if(Next[now][id] == -1) {
            Next[now][id] = newnode();
        }
        now = Next[now][id];
    }
    ++End[now];
}
void get_fail() {
    queue<int>q;
    fail[root] = root;
    for(int i = 0 ; i < 26 ; i++) {
        if(Next[root][i] == -1) {
            Next[root][i] = root;
        }
        else {
            fail[Next[root][i]] = root;
            q.push(Next[root][i]);
        }
    }
    while(!q.empty()) {
        int now = q.front();
        q.pop();
        for(int i = 0 ; i < 26 ; i++) {
            if(Next[now][i] == -1) {
                Next[now][i] = Next[fail[now]][i];
            }
            else {
                fail[Next[now][i]] = Next[fail[now]][i];
                q.push(Next[now][i]);
            }
        }
    }
}
void match(char s[]) {
    int now = root;
    int len = strlen(s);
    for(int i = 0 ; i < len ; i++) {
        int id = s[i] - ‘a‘;
        now = Next[now][id];
        int temp = now;
        while(temp != root) {
            cnt += End[temp];
            End[temp] = 0;
            temp = fail[temp];
        }
    }
}
int main()
{
    int t;
    scanf("%d" , &t);
    while(t--) {
        int n;
        scanf("%d" , &n);
        init();
        for(int i = 0 ; i < n ; i++) {
            scanf("%s" , str);
            build(str);
        }
        get_fail();
        scanf("%s" , sl);
        cnt = 0;
        match(sl);
        printf("%d\n" , cnt);
    }
    return 0;
}

hdu 2222 Keywords Search(ac自动机)

标签:fail   std   using   printf   自动   arch   root   string   原理   

原文地址:http://www.cnblogs.com/TnT2333333/p/6080469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!