AC自动机-HDU3065-简单题

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

需要记录匹配情况的AC自动机,没有清空一些数组导致wa了几发。

/*--------------------------------------------------------------------------------------*/
//        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,ans[2000];
char buf[2000100];
map <int,string > m;

struct Trie
{
    int next[50010][26],fail[50010],end[50010],index[50010];
    int root,L,cnt;
    int newnode()
    {
        for(int i=0;i<26;i++) next[L][i] = -1;
        end[L++] = 0;
        return L-1;
    }

    void init()
    {
        L = 0;
        cnt = 0;
        memset(index,0,sizeof index);
        memset(ans,0,sizeof ans);
        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]++;
        index[now] = cnt++;
    }

    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]);
                }
            }
        }

    }

    void query(char *s)
    {
        int len = strlen(s);
        int now = root;
        for(int i=0;i<len;i++)
        {
            if(s[i]<‘A‘||s[i]>‘Z‘)
            {
                now = root;
                continue;
            }
            now = next[now][s[i]-‘A‘];

            int temp = now;
            while(temp != root)
            {
                ans[index[temp]] += end[temp];
                temp = fail[temp];
            }
        }
    }

    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");
        }
    }
}ac;

int main()
{
    while(~scanf("%d",&N))
    {
        ac.init();
        m.clear();

        for(int i=0;i<N;i++)
        {
            scanf("%s",buf);
            m.insert(pair<int,string>(i,string(buf)));
            ac.insert(buf);
        }
        getchar();
        ac.build();
        scanf("%s",buf);
        ac.query(buf);

        for(int i=0;i<N;i++) if(ans[i])
        {
            printf("%s: %d\n",m[i].c_str(),ans[i]);
        }
    }
}
时间: 2024-08-24 03:34:27

AC自动机-HDU3065-简单题的相关文章

luogu P3808 【模板】AC自动机(简单版)

二次联通门 : luogu P3808 [模板]AC自动机(简单版) /* luogu P3808 [模板]AC自动机(简单版) 手速越来越快了 10分钟一个AC自动机 一遍过编译 + 一边AC 感觉不错 我也就做做板子题了.. */ #include <iostream> #include <cstring> #include <cstdio> #include <queue> #define Max 1000009 void read (int &

【模版】AC自动机(简单版)

题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. 输入输出格式 输入格式: 第一行一个n,表示模式串个数: 下面n行每行一个模式串: 下面一行一个文本串. 输出格式: 一个数表示答案 输入输出样例 输入样例#1: 2 a aa aa 输出样例#1: 2 说明 subtask1[50pts]:∑length(模式串)<=10^6,len

AC自动机(简单版)

觉得AC自动机怪简单是怎么回事?(可能题太裸了) 原题链接:https://www.luogu.org/problemnew/show/P3808 网上讲AC自动机和tire树讲的比我好的dalao数不胜数,我就不多赘述了,权当是挂个板子吧. 其实char和strlen还是挺好用的. #include<cstdio> #include<queue> #include<cstring> using namespace std; struct tire { int fail

模板】AC自动机(简单版)

模板]AC自动机(简单版) https://www.luogu.org/problemnew/show/P3808 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次,请各位注意 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. 输入输出格式 输入格式: 第一行一个n,表示模式串个数: 下面n行每行一个模式串: 下面一行一个文本串. 输

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 /*--------------------

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

P3808 【模版】AC自动机(简单版)

题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. 输入输出格式 输入格式: 第一行一个n,表示模式串个数: 下面n行每行一个模式串: 下面一行一个文本串. 输出格式: 一个数表示答案 输入输出样例 输入样例#1: 2 a aa aa 输出样例#1: 2 说明 subtask1[50pts]:∑length(模式串)<=10^6,len

「LuoguP3808」 【模板】AC自动机(简单版)

题目背景 通过套取数据而直接“打表”过题者,是作弊行为,发现即棕名. 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次,请各位注意 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. 输入输出格式 输入格式: 第一行一个n,表示模式串个数: 下面n行每行一个模式串: 下面一行一个文本串. 输出格式: 一个数表示答案 输入输出样例 输入样

【模板】AC自动机(简单版)

题目背景 通过套取数据而直接“打表”过题者,是作弊行为,发现即棕名. 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次,请各位注意 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. 输入格式 第一行一个n,表示模式串个数: 下面n行每行一个模式串: 下面一行一个文本串. 输出格式 一个数表示答案 输入输出样例 输入 #1复制 2 a