HDU 3695 Computer Virus on Planet Pandora AC自动机

来源:互联网 发布:java finalize 异常 编辑:程序博客网 时间:2024/06/07 09:55

题目链接

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 longusing 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));    } }
阅读全文
0 0
原创粉丝点击