HDU-3065 病毒侵袭持续中 AC自动机!

病毒侵袭持续中

   上一题是求出现多少病毒输出病毒序号,而这题输出每个病毒出现的次数。这题有字典树基础都能做出来,把叶子节点用相应的编号标记起来,匹配的时候遍历到叶子节点用一个数组把次数存起来就行了。

    有了前几题的教训直接用静态树做用C++交,果然不会错!

const int N=129;
char str[2000001],s[1001][55],v[1001];
int head,tail,cnt;
struct tree
{
    int f;
    tree *fail;
    tree *next[N];
}*q[50001],*root,memory[50001];
void insert(int i,char *s)
{
    tree *p=root;
    while(*s!='\0')
    {
        int id=(int)(*s);
        if(p->next[id]==NULL) p->next[id]=&memory[cnt++];
        p=p->next[id];
        s++;
    }
    p->f=i;
}
void build()
{
    root->fail=NULL;
    q[head++]=root;
    while(head!=tail)
    {
        tree *temp=q[tail++];
        tree *p=NULL;
        for(int i=0; i<N; i++)
            if(temp->next[i])
            {
                if(temp==root) temp->next[i]->fail=root;
                else
                {
                    p=temp->fail;
                    while(p!=NULL)
                    {
                        if(p->next[i])
                        {
                            temp->next[i]->fail=p->next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL) temp->next[i]->fail=root;
                }
                q[head++]=temp->next[i];
            }
    }
}
void find(char *s)
{
    tree *p=root;
    int ans=0;
    while(*s!='\0')
    {
        int id=(int)(*s);
        while(p->next[id]==NULL&&p!=root) p=p->fail;
        p=p->next[id];
        p=p==NULL?root:p;
        tree *temp=p;
        while(temp!=root)
        {
            if(temp->f)  v[temp->f]++;
            temp=temp->fail;
        }
        s++;
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(memory,0,sizeof(memory));
        memset(v,0,sizeof(v));
        cnt=head=tail=0;
        root=&memory[cnt++];
        for(int i=1; i<=n; i++)
        {
            scanf("%s",s[i]);
            insert(i,s[i]);
        }
        build();
        scanf("%s",str);
        find(str);
        for(int i=1; i<=n; i++)
            if(v[i]) printf("%s: %d\n",s[i],v[i]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值