HDU2896(病毒侵袭--AC自动机)

题目:病毒侵袭


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>

using namespace std;

const int N=1000050;

char S[N];
char keyword[256];

class Trie
{
    public:
        int index;
        Trie *fail;
        Trie *next[128];
        Trie()
        {
            index=0;
            fail=NULL;
            memset(next,NULL,sizeof(next));
        }
};

Trie *root;
queue<Trie*>Q;

int ss[5500][5],k[5500];

void Insert(char *S,int value)
{
    int len=strlen(S);
    Trie *p=root;
    for(int i=0; i<len; i++)
    {
        int id=S[i]-' ';
        if(p->next[id]==NULL)
            p->next[id]=new Trie();
        p=p->next[id];
    }
    p->index=value;
}

void Build_AC()
{
    Q.push(root);
    root->fail=NULL;
    while(!Q.empty())
    {
        Trie *p=NULL;
        Trie *tmp=Q.front();
        Q.pop();
        for(int i=0; i<128; i++)
        {
            if(tmp->next[i])
            {
                if(tmp==root) tmp->next[i]->fail=root;
                else
                {
                    p=tmp->fail;
                    while(p)
                    {
                        if(p->next[i])
                        {
                            tmp->next[i]->fail=p->next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL) tmp->next[i]->fail=root;
                }
                Q.push(tmp->next[i]);
            }
        }
    }
}

void Query(char *S,int c)
{
    Trie *p=root;
    int flag[550];
    k[c]=0;
    memset(flag,0,sizeof(flag));
    int index;
    int len=strlen(S);
    for(int i=0; i<len; i++)
    {
        index=S[i]-' ';
        while(p->next[index]==NULL&&p!=root) p=p->fail;
        p=p->next[index];
        if(p==NULL) p=root;
        Trie *tmp=p;
        while(tmp!=root&&!flag[tmp->index])
        {
            if(tmp->index)
            {
                ss[c][k[c]++]=tmp->index;
                flag[tmp->index]=1;
            }
            tmp=tmp->fail;
        }
        if(k[c]>=3) break;
    }
}

int main()
{
    int t,n,m;
    while(~scanf("%d",&n))
    {
        root=new Trie();
        getchar();
        for(int i=1; i<=n; i++)
        {
            gets(keyword);
            Insert(keyword,i);
        }
        Build_AC();
        scanf("%d",&m);
        int total=0;
        for(int i=1; i<=m; i++)
        {
            scanf("%s",S);
            Query(S,i);
            if(k[i])
            {
                sort(ss[i],ss[i]+k[i]);
                total++;
            }
        }
        for(int i=1; i<=m; i++)
        {
            if(!k[i]) continue;
            printf("web %d:",i);
            for(int j=0; j<k[i]; j++)
                printf(" %d",ss[i][j]);
            puts("");
        }
        printf("total: %d\n",total);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值