ac自动机模板

hdu2896

#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <algorithm>

using namespace std;

const int wl = 128;
struct Node
{
    Node *fail;
    Node *next[wl];
    int counts, id;
    Node()
    {
        counts = id = 0;
        fail = NULL;
        for (int i = 0; i < wl; i++)
        {
            next[i] = NULL;
        }
    }
}*q[100000 + 5];

struct Web
{
    int num;
    int temp[5];
}web[1000 + 10];

char keyword[200 + 10];
char str[10000 + 10];
int head, tail, tot;

void inserts(char *s, Node *root)
{
    int i = 0;
    Node *p = root;

    while (s[i])
    {
        int t = s[i];

        if (p->next[t] == NULL)
        {
            p->next[t] = new Node();
        }

        p = p->next[t];
        i++;
    }

    p->counts++;
    p->id = tot;
}

void build_ac_automation(Node *root)
{
    root->fail = NULL;
    q[tail++] = root;

    while (head != tail)
    {
        Node *temp = q[head++];
        Node *p = NULL;

        for (int i = 0; i < 128; i++)
        {
            if (temp->next[i] != NULL)
            {
                if (temp == root)
                {
                    temp->next[i]->fail = root;
                }
                else
                {
                    p = temp->fail;

                    while (p != NULL)
                    {
                        if (p->next[i] != NULL)
                        {
                            temp->next[i]->fail = p->next[i];
                            break;
                        }
                        p = p->fail;
                    }

                    if (p == NULL)
                    {
                        temp->next[i]->fail = root;
                    }
                }

                q[tail++] = temp->next[i];
            }
        }
    }
}

int query(Node *root)
{
    head = tail = 0;
    int len = strlen(str), i = 0;
    int cnt = 0;
    Node *p = root;
    bool flag[500 + 10];
    memset(flag, false, sizeof(flag));

    while (str[i])
    {
        int t = str[i];
        while (p->next[t] == NULL && p != root)
        {
            p = p->fail;
        }

        p = p->next[t];
        if (p == NULL)
        {
            p = root;
        }

        Node *temp = p;

        while (temp != root && !flag[temp->id])
        {
            if (temp->counts)
            {
                web[tot].num++;
                web[tot].temp[cnt++] = temp->id;
                flag[temp->id] = true;
            }
            temp = temp->fail;
        }
        i++;
    }

    return cnt;
}

void input()
{
    int n, m;

    while (scanf("%d", &n) != EOF)
    {
        tot = 0;
        Node *root = new Node();
        scanf("%d", &n);

        while (n--)
        {
            gets(keyword);
            inserts(keyword, root);
            tot++;
        }

        build_ac_automation(root);

        tot = 0;
        memset(web, 0, sizeof(web));
        scanf("%d", &m);
        getchar();

        while (m--)
        {
            gets(str);
            query(root);
            tot++;
        }

        int ans = 0;

        for (int i = 0; i < tot; i++)
        {
            if (web[i].num != 0)
            {
                ans++;
                printf("web %d:", i + 1);

                sort(web[i].temp, web[i].temp + web[i].num);
                for (int j = 0; j < web[i].num; j++)
                {
                    printf(" %d", web[i].temp[j] + 1);
                }
                printf("\n");
            }
        }

        printf("total: %d\n", ans);
    }
}

int main()
{
    input();
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值