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

题意:

题解:

//先调用PreProcess()初始化
//Insert()添加串  Find()查找串
//get(char s) 返回字符s所对应的编号(考虑字符集合可能是’A’-’Z’或者”ACGT”等)
//全部Insert后调用 bfs()求fail指针

#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;

#define CN 26
#define MAXN 1000010

struct Trie
{
    int next[CN];
    int fail;
    int cnt;
    bool flag;

    void init()
    {
        flag = true;
        fail = cnt = 0;
        memset(next, -1, sizeof(next));
    }
} node[MAXN];

int len;

int get(char ch)
{
    return ch - 'a';
}

void PreProcess()
{
    len = 0;
    node[0].init();
    node[0].fail = -1;
}

void Insert(char *str)
{
    int r = 0, i, id;
    for(i = 0; str[i] != '\0'; i++)
    {
        id = get(str[i]);
        if(node[r].next[id] == -1)
        {
            ++len;
            node[len].init();
            node[r].next[id] = len;
        }
        r = node[r].next[id];
    }
    node[r].flag = false;
    node[r].cnt++;
}

void BFS()
{
    queue<int> q;
    q.push(0);
    int f, nextId, i, fail;
    while(!q.empty())
    {
        f = q.front();
        q.pop();
        for(i = 0; i < CN; i++)
        {
            nextId = node[f].next[i];
            if(nextId != -1)
            {
                q.push(nextId);
                fail = node[f].fail;
                while(fail != -1 && node[fail].next[i] == -1)
                    fail = node[fail].fail;
                if(fail == -1)
                    node[nextId].fail = 0;
                else
                    node[nextId].fail = node[fail].next[i];
            }
        }
    }
}

int Find(char *str)
{
    int ret = 0;
    int r = 0, nextId, id;
    for(int i = 0; str[i]; )
    {
        id = get(str[i]);
        nextId = node[r].next[id];

        if(nextId != -1)
        {
            int tmp = nextId;
            while(tmp != 0 && node[tmp].cnt != 0)
            {
                ret += node[tmp].cnt;
                node[tmp].cnt = 0;
                tmp = node[tmp].fail;
            }

            r = nextId, ++i;
        }
        else
        {
            r = node[r].fail;
            if(r == -1) { ++i; ++r; }
        }
    }
    return ret;
}


int main()
{
    int cs, n;
    char key[100];
    char desc[1000010];
    scanf("%d",&cs);
    while(cs--)
    {
        PreProcess();
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",key);
            Insert(key);
        }
        scanf("%s",desc);
        BFS();
        printf("%d\n",Find(desc));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值