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("
");for(int i=0;i<(N);i++)
{for(int j=0;j<(M);j++){
printf("%d",G[i][j]);}printf("
");}                
//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("]
");
        }
    }
}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
",m[i].c_str(),ans[i]);
        }
    }
}
原文地址:https://www.cnblogs.com/helica/p/5037820.html