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

HDOJ - 2222 [AC自动机]

时间:2014-07-31 02:18:15      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   2014   代码   

此题为AC自动机的裸题。

代码如下:

/**************************************
Problem : HDOJ - 2222
Author : Magician Van
Method : AC-Automation
Time : 07/30/2014
**************************************/

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

const int MaxL = 1000000 + 5, SmallL = 50 + 5, MaxKind = 26 + 5, MaxNode = 50 * 10000 + 10;

int T, n, Ans, top;

char Str[MaxL], Ss[SmallL];

typedef struct TrieNode
{
    int count;
    TrieNode *Next[MaxKind], *fail;
    void clear()
    {
        count = 0;
        fail = NULL;
        for (int i = 0; i <= 25; i++) Next[i] = NULL;
    }
} Trie;

Trie *root, Ta[MaxNode];

void Insert(char *S, int len)
{
    Trie *p = root;
    int t;
    for (int i = 0; i <= len - 1; i++)
    {
        t = S[i] - a;
        if (p -> Next[t] == NULL)
        {
            Trie *temp = &Ta[++top];
            temp -> clear();
            p -> Next[t] = temp;
        }
        p = p -> Next[t];
    }
    p -> count++;
}

queue<Trie *> Q;

void Build_AC_Automation()
{
    root -> fail = NULL;
    while (!Q.empty()) Q.pop();
    Q.push(root);
    Trie *now, *p;
    while (!Q.empty())
    {
        now = Q.front();
        Q.pop();
        for (int i = 0; i <= 25; i++)
        {
            if (now -> Next[i] == NULL) continue;
            if (now == root)
            {
                now -> Next[i] -> fail = root;
            }
            else
            {
                p = now -> fail;
                while (p != NULL)
                {
                    if (p -> Next[i] != NULL)
                    {
                        now -> Next[i] -> fail = p -> Next[i];
                        break;
                    }
                    p = p -> fail;
                }
                if (p == NULL) now -> Next[i] -> fail = root;
            }
            Q.push(now -> Next[i]);
        }
    }
}

void Query(char *S, int len)
{
    int t;
    Trie *now = root, *temp;
    for (int i = 0; i <= len - 1; i++)
    {
        t = S[i] - a;
        while (now != root && now -> Next[t] == NULL) now = now -> fail;
        now = now -> Next[t];
        if (now == NULL) now = root;
        temp = now;
        while (temp != NULL && temp -> count != -1)
        {
            Ans += temp -> count;
            temp -> count = -1;
            temp = temp -> fail;
        }
    }
}

inline void read_int(int &num)
{
    char c; while (c = getchar(), c < 0 || c > 9);
    num = c - 0; while (c = getchar(), c >= 0 && c <= 9) num = num * 10 + c - 0;
}

int main()
{
    read_int(T);
    for (int Case = 1; Case <= T; Case++)
    {
        Ans = 0;
        top = 0;
        root = &Ta[++top];
        root -> clear();
        read_int(n);
        for (int i = 1; i <= n; i++)
        {
            scanf("%s", Ss);
            Insert(Ss, strlen(Ss));
        }
        scanf("%s", Str);
        Build_AC_Automation();
        Query(Str, strlen(Str));
        printf("%d\n", Ans);
    }
    return 0;
}

 

HDOJ - 2222 [AC自动机],布布扣,bubuko.com

HDOJ - 2222 [AC自动机]

标签:style   blog   color   os   io   for   2014   代码   

原文地址:http://www.cnblogs.com/F-Magician/p/3879573.html

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