hdu3065ac自动机

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<cstring>
using namespace std;
const int maxn=5000100;
char str[2000050],key[55];
struct hh
{
    string ss;
    int num;
    hh()
    {
        ss="";
        num=0;
    }
}ans[1005];
struct node
{
    node *fail;
    node *next[28];
    int count,k;
    string str;
    node()
    {
        fail=NULL;
        count=0;k=0;
        memset(next,NULL,sizeof(next));
        str="";
    }
};
node *root,*q[maxn];
int head,tail;
void InSert(char *str,int k)
{
    int i=0;
    node *p=root;
    while(str[i])
    {
        int id=str[i]-'A';
        if(p->next[id]==NULL) p->next[id]=new node();
        p=p->next[id];
        i++;
    }
    p->count++;
    p->str=str;
    p->k=k;
}
void Build_ac_Bfs()
{
    q[tail++]=root;
    while(head!=tail)
    {
        node *p=q[head++];
        node *tmp=NULL;
        for(int i=0;i<26;i++)
        {
            if(p->next[i]==NULL) continue;
            if(p==root) p->next[i]->fail=root; 
            else
            {
                tmp=p->fail;
                while(tmp!=NULL)
                {
                    if(tmp->next[i]!=NULL)
                    {
                        p->next[i]->fail=tmp->next[i];
                        break;
                    }
                    tmp=tmp->fail;
                }
                if(tmp==NULL) p->next[i]->fail=root;
            }
            q[tail++]=p->next[i];
        }
    }
}
void dis()
{
    for(int i=0;i<1002;i++)
    {
        if(ans[i].num!=0)
         cout<<ans[i].ss<<": "<<ans[i].num<<endl;
    }
}
void Query()
{
    int id,len=strlen(str);
    node *p=root;
    for(int i=0;i<len;i++)
    {
        if(str[i]<'A'||str[i]>'Z') { p=root;continue;} //如果其他字符,则指向root。
        id=str[i]-'A';
        while(p->next[id]==NULL&&p!=root) p=p->fail;
        p=p->next[id];
        if(p==NULL) p=root;
        node *tmp=p;
        while(tmp!=root)
        {
            if(tmp->count>0)
            {
                ans[tmp->k].ss=tmp->str;
                ans[tmp->k].num++;
            }
            tmp=tmp->fail;
        }
    }
    dis();
}
void Del(node *p)
{
    for(int i=0;i<26;i++)
      if(p->next[i]!=NULL)
        Del(p->next[i]);
    delete p;
}
int main()
{
    //freopen("Input.txt","r",stdin);
    int num;
    while(~scanf("%d",&num))
    {
        head=tail=0;
        for(int i=0;i<1002;i++) //忘记初始化为零,wa了6次。
         ans[i].num=0;
        root=new node();
        getchar();
        for(int i=1;i<=num;i++)
        {
            gets(key);
            InSert(key,i);
        }
        Build_ac_Bfs();
        scanf("%s",str);
        Query();
        Del(root);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值