hdu 2222

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
struct tree
{
    int count;
    tree *next[26];
    tree *fail;
};
stack < tree* > Q;//此处错误,应该是队列而非栈,未修改
char keyword[55];
char str[1111111];
tree *root;
void insert(tree *root,char *word)
{
    int i,j,k,l,m,n;
    int index;
    tree *p=root,*newnode;
    l=strlen(word);
    for(i=0; i<l; i++)
    {
        index=word[i]-'a';
        if(!p->next[index])
        {
            newnode=new tree;
            for(j=0; j<26; j++)
            {
                newnode->next[j]=NULL;
            }
            newnode->count=0;
            newnode->fail=NULL;
            p->next[index]=newnode;
        }
        p=p->next[index];
    }
    p->count++;
}
void failed(tree *root)
{
    int i,j,k,l,m,n;
    while(!Q.empty())
    {
        Q.pop();
    }
    Q.push(root);
    tree *temp,*p;
    while(!Q.empty())
    {
        temp=Q.top();
        Q.pop();
        for(i=0; i<26; i++)
        {
            if(temp->next[i])
            {
                if(temp==root)
                    temp->next[i]->fail=root;
                else
                {
                    p=temp->fail;
                    while(p)
                    {
                        if(p->next[i])
                        {
                            temp->next[i]->fail=p->next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL)
                        temp->next[i]->fail=root;
                }
                Q.push(temp->next[i]);
            }
        }
    }
}
int query(tree *root)
{
    int i,j,k,l,m,n;
    int cnt=0;
    int index;
    tree *p=root;
    l=strlen(str);
    for(i=0; i<l; i++)
    {
        index=str[i]-'a';
        while(!p->next[index]&&p!=root)
            p=p->fail;
        p=p->next[index];
        if(!p)
            p=root;
        tree *temp=p;
        while(temp!=root)
        {
            if(temp->count>=0)
            {
                cnt+=temp->count;
                temp->count=-1;
            }
            else break;
            temp=temp->fail;
        }
    }
    return cnt;
}
int del(tree *T)
{
    int i;
    if(T==NULL)
        return 0;
    for(i=0;i<26;i++)
    {
        if(T->next[i]!=NULL)
            del(T->next[i]);
    }
    delete T;
    return 0;
}
int main()
{
    int i,j,k,l,m,n;
    int t;
    cin>>t;
    while(t--)
    {
        root=new tree;
        for(i=0; i<26; i++)
        {
            root->next[i]=NULL;
        }
        root->fail=NULL;
        root->count=0;
        cin>>n;
        for(i=0; i<n; i++)
        {
            cin>>keyword;
            insert( root , keyword );
        }

        failed(root);
        cin>>str;
        k=query(root);
        del(root);
        cout<<k<<endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值