AC自动机-HDU2222-模板题

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

一个AC自动机的模板题。用的kuangbin的模板,静态建Trie树。可能遇到MLE的情况要转动态建树。

AC自动机的讲解看这里

http://blog.csdn.net/niushuai666/article/details/7002823

http://blog.csdn.net/mobius_strip/article/details/22549517

/*--------------------------------------------------------------------------------------*/
//        Helica‘s header
//        Second Editions
//        2015.11.7
//
#include <algorithm>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>

//debug function for a N*M array
#define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\
{for(int j=0;j<(M);j++){printf("%d",G[i][j]);}printf("\n");}
//debug function for int,float,double,etc.
#define debug_var(X) cout<<#X"="<<X<<endl;
/*--------------------------------------------------------------------------------------*/
using namespace std;

int N,M,T;

struct Trie
{
    int next[500010][26],fail[500010],end[500010];
    int root,L;
    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 insert(char *s)
    {
        int len = strlen(s);
        int now = root;
        for(int i=0;i<len;i++)
        {
            if(next[now][s[i]-‘a‘] == -1)
                next[now][s[i]-‘a‘] = newnode();
            now = next[now][s[i]-‘a‘];
        }
        end[now]++;
    }

    void build()
    {
        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]);
                }
            }
        }

    }

    int query(char *s)
    {
        int len = strlen(s);
        int now = root;
        int ans = 0;
        for(int i=0;i<len;i++)
        {
            now = next[now][s[i]-‘a‘];
            int temp = now;
            while(temp != root)
            {
                ans += end[temp];
                end[temp] = 0;
                temp = fail[temp];
            }
        }
        return ans;
    }
    void debug()
    {
        for(int i=0;i<L;i++)
        {
            printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
            for(int j=0;j<26;j++)
                printf("%2d",next[i][j]);
            printf("]\n");
        }
    }
};

char buf[1000100];
Trie ac;

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        ac.init();
        scanf("%d",&N);
        for(int i=0;i<N;i++)
        {
            scanf("%s",buf);
            ac.insert(buf);
        }
        ac.build();
        //ac.debug();
        scanf("%s",buf);
        printf("%d\n",ac.query(buf));
    }
}

第N次入门AC自动机。。。成功。。。

时间: 2024-10-13 19:45:57

AC自动机-HDU2222-模板题的相关文章

AC自动机-HDU2896-模板题

http://acm.hdu.edu.cn/showproblem.php?pid=2896 另一道AC自动机的模板题,不过这题需要记录一下具体的匹配情况. /*--------------------------------------------------------------------------------------*/ // Helica's header // Second Editions // 2015.11.7 // #include <algorithm> #inc

hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/128000 K (Java/Others)Total Submission(s): 2578    Accepted Submission(s): 713 Problem Description Aliens on planet Pandora also write computer programs l

ac自动机基础模板(hdu2222)

In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey also wants to bring this feature to his image retrieval system. Every image have a long description, when users type some keywords to find the image, th

[hdu2222]ac自动机(模板)

题意:一个文本串+多个模板串的匹配问题 思路:裸的ac自动机. 1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map&g

HDU 2222 Keywords Search (AC自动机入门 模板)

AC自动机入门 Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一.学习AC自动机之前得先有Trie树和KMP模式匹配算法的基础. AC自动机算法分为3步:1.构造一棵tire树  2.构造失败指针  3.进行模式匹配 AC自动机的优化:Trie图 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other

AC自动机【模板】+经典例题

AC自动机模板 经典例题 Keywords Search HDU - 2222 [求目标串中出现了几个模式串] [(注意:模式串可能会重复)] 模板: 1 const int maxn=26; 2 struct Trie 3 { 4 int next[500010][maxn]; 5 int fail[500010],end[500010]; 6 int root,L; 7 int newnode() //初始化 8 { 9 for(int i=0;i<26;i++) 10 next[L][i]

AC自动机 HDU2222 Keywords Search

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 62674    Accepted Submission(s): 20749 Problem Description In the modern time, Search engine came into the life of everybody lik

AC自动机-HDU3065-简单题

http://acm.hdu.edu.cn/showproblem.php?pid=3065 需要记录匹配情况的AC自动机,没有清空一些数组导致wa了几发. /*--------------------------------------------------------------------------------------*/ // Helica's header // Second Editions // 2015.11.7 // #include <algorithm> #inc

NYOJ 1085 AC自动机基础模板

今天学了AC自动机,可以说AC自动机是把匹配的串建立成为一颗trie,然后就和kmp 是一样的 题意:判断在一篇文章中有多少单词出现过,并输出来 #include<cstdio> #include<cstring> #include<map> #include<queue> #include<iostream> using namespace std; const int maxn = 1000007; int cnt; struct Node{