HDU 3695 Computer Virus on Planet Pandora AC自动机

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=3695

题意

外星人的程序全是由大小写的英文字母组成,外星人也会写病毒程序。病毒程序也是大小写的英文字母组成,如果病毒是某个程序的子串,或者病毒的反转串是程序的子串,那么就说程序被该病毒感染。给出一个病毒及一个程序,问程序被之中的几个病毒感染了。

思路

将程序解析成字符串,正反在查询即可。。解析函数写错导致无限WA(╥╯^╰╥)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define ll long long
using namespace std;
const int INF = ( 2e9 ) + 2;
const ll maxn = 5100010;
struct node
{
    node *next[26];
    node *fail;
    int f,cnt;
}t[maxn+100000],*root,*que[maxn];
int tot,head,tail;
char v[1010];
char s1[maxn],s2[maxn];
node *create()
{
    t[tot].cnt=t[tot].f=0;
    t[tot].fail=NULL;
    for(int i=0;i<26;i++)
    t[tot].next[i]=NULL;
    return &t[tot++];
}
void init()
{
    tot=0;
    root=create();
}
void insert(node *root,char *str)
{
    int i=0;
    node *p=root;
    while(str[i])
    {
        int index = str[i]-'A';
        if(p->next[index]==NULL)
        p->next[index]=create();
        p=p->next[index];
        i++;
    }
    p->cnt++,p->f=1;
}
void build_AC(node *root)
{
    head=tail=0;
    que[head++]=root;
    while(tail!=head)
    {
        node *cur=que[tail++];
        tail = tail%maxn;
        for(int i=0;i<26;i++)
        if(cur->next[i])
        {
            if(cur==root)cur->next[i]->fail=root;
            else
            {
                cur->next[i]->fail=cur->fail->next[i];
                if(!cur->next[i]->f)
                cur->next[i]->cnt=1;
            }
            que[head++]=cur->next[i];
            head = head%maxn;
        }
        else
        {
            if(cur==root)cur->next[i]=root;
            else cur->next[i]=cur->fail->next[i];
        }
    }
}
int AC_automation(node *root,char *str)
{
    int i=0,ret=0;
    node *p=root;
    while(str[i])
    {
        int index=str[i]-'A';
        p=p->next[index];
        if(p->cnt)
        {
            node *temp=p;

            while(temp!=root)
            {
                if(temp->f)ret+=temp->cnt;
                    temp->cnt=0;
                    temp=temp->fail;
            }
        }
        i++;
    }
    return ret;
}
void decompressed()
{
    int len=strlen(s1);
    int cnt=0,q=0;
    bool mode=0;
    for(int i=0;i<len;i++)
    {
        if(s1[i]==']')
        {
            for(int k=0;k<q;k++)
            s2[cnt++]=s1[i-1];
            q=0;
            mode=0;
            continue;
        }
        if(mode)
        {
            if(s1[i]>='0'&&s1[i]<='9')
            q = q*10+s1[i]-'0';
            continue;
        }
        if(s1[i]=='[')
        {
            mode=1;
            continue;
        }
        s2[cnt++]=s1[i];
    }
    s2[cnt]=s1[cnt]='\0';
    q=cnt;
    for(int i=0;i<cnt;i++)
    s1[i]=s2[--q];
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        init();
        for(int i=0;i<n;i++)
        {
            scanf("%s",v);
            insert(root,v);
        }
        build_AC(root);
        scanf("%s",s1);
        decompressed();
        printf("%d\n",AC_automation(root,s1)+AC_automation(root,s2));
    } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值