HDOJ 2222

思路: AC自动机


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Node_Tree
{
    int cnt; 
    struct Node_Tree *child[26]; 
    struct Node_Tree *fail; 
}Node; 
Node *root; 
char keywd[51]; 
char decpt[1000001]; 
Node *q[500001]; 
int tail = 0, head = 0; 
void insert()
{
    if(keywd == NULL)
        return ; 
    int i; 
    char *p = keywd;
    Node *t = root;
    while(*p != '\0')
    {
        if(t->child[*p - 'a'] == NULL)
        {
            Node *temp = (Node *)malloc(sizeof(Node)); 
            memset(temp, 0, sizeof(Node)); 
            for(i = 0; i < 26; i ++)
            {
                temp->child[i] = NULL; 
            }
            temp->cnt = 0;
            temp->fail = NULL; 
            t->child[*p - 'a'] = temp; 
        }
        t = t->child[*p - 'a']; 
        p ++; 
    }
    t->cnt ++; 
}

void getfail()
{
    int i; 
    q[tail++] = root;
    while(tail != head)             //BFS; 
    {
        Node *p = q[head++];
        Node *temp = NULL;
        for(i = 0; i < 26; i ++)
        {
            if(p->child[i] != NULL)
            {
                if(p == root)
                {
                    p->child[i]->fail = root; 
                }
                else
                {
                    temp = p->fail; 
                    while(temp != NULL)
                    {
                        if(temp->child[i] != NULL)
                        {
                            p->child[i]->fail = temp->child[i]; 
                            break ; 
                        }
                        temp = temp->fail; 
                    }
                    if(temp == NULL)
                        p->child[i]->fail = root; 
                }
                q[tail++] = p->child[i]; 
            }
        }
    }
}

int search()
{
    int i, ret = 0; 
    char *p = decpt; 
    Node *t = root;
    while(*p != '\0')
    {
        while(t->child[*p - 'a'] == NULL && t != root)
            t = t->fail; 
        t = t->child[*p - 'a']; 
        if(t == NULL)
            t = root;
        Node *temp = t; 
        while(temp != root && temp->cnt != -1)
        {
            ret += temp->cnt;
            temp->cnt = -1;
            temp = temp->fail;
        }
        p ++; 
    }
    return ret; 
}

int main(int argc, char const *argv[]) 
{
    int c, i, t; 
    scanf("%d", &c);
    Node TREEROOT; 
    root = &TREEROOT; 
    while(c --)
    {
        for(i = 0; i < 30; i ++)
        {
            root->child[i] = NULL; 
            root->cnt = 0;
            root->fail = NULL; 
        }
        tail = head = 0; 
        memset(decpt, 0, sizeof(decpt));
        memset(keywd, 0, sizeof(keywd)); 
        scanf("%d", &t); 
        while(t --)
        {
            scanf("%s", keywd); 
            insert(); 
            memset(keywd, 0, sizeof(keywd)); 
        }
        getfail(); 
        scanf("%s", decpt);
        printf("%d\n", search());
    }
    return 0; 
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值