HDU 2896 AC自动机

G++交MLE C++交AC

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<queue>
using namespace std;
struct node
{
    node *next[128];
    node *fail;
    int num;
    node()
    {
        num=0;
        fail=NULL;
        for(int i=0; i<128; i++)
            next[i]=NULL;
    }
}*root;
void Insert(char *str,int id)
{
    node *p=root;
    int temp;
    int len=strlen(str);
    for(int i=0; i<len; i++)
    {
        temp=str[i];
        if(p->next[temp]==NULL)
            p->next[temp]=new node();
        p=p->next[temp];
    }
    p->num=id;
}
void build_ac()
{
    node *p,*temp,*son;
    queue<node *>Q;
    Q.push(root);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        for(int i=0; i<128; i++)
        {
            son=p->next[i];
            if(son)
            {
                if(p==root)
                    son->fail=root;
                else
                {
                    temp=p->fail;
                    while(temp)
                    {
                        if(temp->next[i])
                        {
                            son->fail=temp->next[i];
                            break;
                        }
                        temp=temp->fail;
                    }
                    if(temp==NULL)
                        son->fail=root;
                }
                Q.push(son);
            }
        }
    }
}
char str[80000];
bool vis[1000];
int n,m;
bool query(int id)
{
    memset(vis,0,sizeof(vis));
    node *p=root,*temp;
    int len=strlen(str);
    int index,flag=0;
    for(int i=0; i<len; i++)
    {
        index=str[i];
        while(p->next[index]==NULL&&p!=root)
            p=p->fail;
        p=p->next[index];
        if(p==NULL)
            p=root;
        temp=p;
        while(temp!=root)
        {
            if(temp->num)
            {
                vis[temp->num]=true;
                flag++;
            }
            temp=temp->fail;
            if(flag==3)
                break;
        }
        if(flag==3)
            break;
    }
    if(flag)
    {
        printf("web %d:",id);
        for(int i=1; i<=n; i++)
            if(vis[i])
                printf(" %d",i);
        printf("\n");
        return true;
    }
    return false;
}
char s[300];
void Dele(node *r)
{
    for(int i=0;i<128;i++)
        if(r->next[i])
     Dele(r->next[i]);
    delete r;
}
int main()
{
    while(~scanf("%d",&n))
    {
        root=new node();
        for(int i=1; i<=n; i++)
        {
            scanf("%s",s);
            Insert(s,i);
        }
        scanf("%d",&m);
        int num=0;
        build_ac();  ///别漏了调用build_ac();
        for(int i=1; i<=m; i++)
        {
            scanf("%s",str);
            if(query(i))
                num++;
        }
        printf("total: %d\n",num);
         //Dele(root);
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值