码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2896 AC自动机

时间:2016-05-07 10:46:49      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

HDU 2896 AC自动机

标签:

原文地址:http://blog.csdn.net/became_a_wolf/article/details/51330566

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!