hdu2896 AC自动机

#include<iostream>
#include<cstdio>
#include<queue>
#include<string.h>
#include<algorithm>
#define kind 94
using namespace std;
struct node
{
    int pox;
    struct node *fail;
    struct node *next[kind];
    node()
    {
        pox=0;
        for(int i=0; i<kind; i++)
            next[i]=NULL;
    }
};
char st[210],st1[10010];
int ans[10],s=0,flag=0;
int cmp(int a,int b)
{
    return a<b;
}
void buildTree(char *word,struct node *root,int pox)
{
    int len=strlen(word);
    int index;
    struct node *temp=root;
    for(int i=0; i<len; i++)
    {
        index=word[i]-32;
        if(temp->next[index]==NULL)
            temp->next[index]=new node();
        temp=temp->next[index];
    }
    temp->pox=pox;
}
void BFS(struct node *root)
{
    queue<struct node*>q;
    struct node *temp,*p;
    root->fail=NULL;
    q.push(root);
    while(!q.empty())
    {
        temp=q.front();
        q.pop();
        for(int i=0; i<kind; i++)
        {
            if(temp->next[i]!=NULL)
            {
                if(temp==root)temp->next[i]->fail=root;
                else
                {
                    p=temp->fail;
                    while(p!=NULL)
                    {

                        if(p->next[i]!=NULL)
                        {
                            temp->next[i]->fail=p->next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL)
                        temp->next[i]->fail=root;
                }
                q.push(temp->next[i]);
            }
        }
    }

}
void find(struct node *root)
{
    int cnt=0,index;
    int len=strlen(st1);
    struct node *p=root;
    for(int i=0; i<len; i++)
    {
        index=st1[i]-32;
        while(p->next[index]==NULL&&p!=root)
            p=p->fail;
        p=(p->next[index]==NULL?root:p->next[index]);
        struct node *temp=p;
        while(temp!=root)
        {
            if(temp->pox!=0)
            {

                flag=1;
                ans[s]=temp->pox;
                s++;
                break;
            }
            temp=temp->fail;
          //  printf("sdfsdf\n");
        }
    }
}
int main()
{
    int N,M;
    scanf("%d",&N);
    struct node *root=new node();
    for(int i=1; i<=N; i++)
    {
        scanf("%s",st);
        buildTree(st,root,i);
    }
    BFS(root);
    scanf("%d",&M);
    int t;
    int sum=0;
    for(int i=1; i<=M; i++)
    {
        flag=0;
        scanf("%s",st1);
        find(root);
        if(flag)
        {
            sum++;
            printf("web %d:",i);
            sort(ans,ans+s,cmp);
            for(int i=0;i<s;i++)
             printf(" %d",ans[i]);
            printf("\n");
        }

      s=0;
        memset(ans,0,sizeof(ans));
    }
    printf("total: %d\n",sum);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值