HDU3695 Computer Virus on Planet Pandora AC自动机模板

来源:互联网 发布:yy淘宝刷钻平台网址 编辑:程序博客网 时间:2024/04/30 14:39

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


10fuzhou

题意:有T组测试数据,每组有n个病毒特征码。和一个字符串,求此字符串正反顺序共含有多少种病毒。


AC自动机,将需要判断的字串正反序均判断一下即可。


#include <cstdio>#include <cstring>#include <cmath>#include <map>#include <set>#include <vector>#include <iostream>#include <algorithm>#include<string>using namespace std;const double eps=1e-7;//const double INF=1e50;//const double pi=acos(-1);#define N 1005#define M 5100005char st[N],str1[M];const int kind = 26;struct node{    node *fail;       //失败指针    node *next[kind]; //Tire每个节点的26个子节点(最多26个字母)    int count;        //是否为该单词的最后一个节点    node(){           //构造函数初始化       fail=NULL;       count=0;       memset(next,NULL,sizeof(next));   }}*q[250005];          //队列,方便用于bfs构造fail指针//char keyword[51];     //输入的单词char str[M];    //模式串int head,tail;        //队列的头尾指针void insert(char *str,node *root){     //建字典树    node *p=root;    int i=0,index;    while(str[i]){        index=str[i]-'A';        if(p->next[index]==NULL) p->next[index]=new node();        p=p->next[index];        i++;    }    p->count++;            //每个单词的末尾字母标记count为1,代表一个单词}/*在字典树上构造fail指针。构造失败指针的过程概括起来就一句话:设这个节点上的字母为C,沿着他父亲的失败指针走,直到走到一个节点,他的儿子中也有字母为C的节点。然后把当前节点的失败指针指向那个字母也为C的儿子。如果一直走到了根节点都没找到,那就把失败指针指向根节点。所以构造fail指针 需要用到BFS。 保证是按层遍历字典树。*/void build_ac_automation(node *root){      //构建fail指针    int i;    root->fail=NULL;       //根节点fail指针指向空值    q[head++]=root;        //根节点入队    while(head!=tail){        node *temp=q[tail++];        node *p=NULL;        for(i=0;i<26;i++){            if(temp->next[i]!=NULL){                if(temp==root) temp->next[i]->fail=root;                //根节点每个儿子的fail指针为根节点                else{                    p=temp->fail;                    while(p!=NULL){    //P不为空,即未到达根节点                        if(p->next[i]!=NULL){  //找到了包含当前字母儿子的父节点                            temp->next[i]->fail=p->next[i];//将子结点的fail指针指向此节点                            break;                        }                        p=p->fail;     //未找到,则到其fail指针节点处继续找                    }                    if(p==NULL) temp->next[i]->fail=root;                }                q[head++]=temp->next[i];   //子节点入队            }        }    }}/*匹配过程分两种情况:(1)当前字符匹配,表示从当前节点沿着树边有一条路径可以到达目标字符,此时只需沿该路径走向下一个节点继续匹配即可,目标字符串指针移向下个字符继续匹配;(2)当前字符不匹配,则去当前节点失败指针所指向的字符继续匹配,匹配过程随着指针指向root结束。重复这2个过程中的任意一个,直到模式串走到结尾为止。*/int query(node *root){    int i=0,cnt=0,index,len=strlen(str);    node *p=root;    while(str[i]){        index=str[i]-'A';        while(p->next[index]==NULL && p!=root) p=p->fail; //当在字典树上找不到c字符,那么就根据fail指针回退       //直到找到 或者到达根节点        p=p->next[index];        p=(p==NULL)?root:p;        node *temp=p;        while(temp!=root && temp->count!=-1){  //根据fail指针回退,直到根节点            cnt+=temp->count;            temp->count=-1;        //避免重复记录            temp=temp->fail;        }        i++;    }    return cnt;}int value(int p,int q){    int i,ans=0,w=1;    for (i=q;i>=p;i--)    {        ans+=(str1[i]-'0')*w;        w*=10;    }    return ans;}int main(){    //freopen("a","r",stdin);    int T,kk,i,n,j,l,k;    scanf("%d",&T);    for (kk=1;kk<=T;kk++)    {        scanf("%d\n",&n);        head=tail=0;        node *root=new node();        for (i=1;i<=n;i++)        {            gets(st);            insert(st,root);        }        build_ac_automation(root);        gets(str1);        l=strlen(str1);        j=0;        for (i=0;i<l;)        {            if (str1[i]!='[') str[j++]=str1[i++];            else            {                for (k=i+1;str1[k]!=']';k++);                int v=value(i+1,k-2);                for (int k1=1;k1<=v;k1++) str[j++]=str1[k-1];                i=k+1;            }        }        str[j]='\0';        //printf("%s\n",str);        int h=query(root);        char chh;        l=strlen(str);        for (i=0;i<=(l-1)/2;i++)        {            chh=str[l-i-1];            str[l-i-1]=str[i];            str[i]=chh;        }        //printf("%s",str);        h+=query(root);        printf("%d\n",h);    }    return 0;}