HDU2222

http://www.elijahqi.win/2017/07/07/hdu2222/ ‎
因为blog主还不会ac自动机
题解待填坑

#include
#include
int const N1=55;
int const N2=1100000;
int case1;
char str1[N1],str2[N2];
int idh,n,ans;
struct node{
    int word,id,ch2;
    char ch1;
    node *next[27],*fail;
    node(){
        id=0;
        word=0;
        ch1=' ';
        fail=NULL;
        ch2=0;
        for (int i=1;i<=26;++i) next[i]=NULL;
    }
};
node *root;
void insert1(char s[]){
    int l=strlen(s);
    node *p=root;
    for (int i=0;iint x=s[i]-'a'+1;
        if (p->next[x]==NULL) {
            p->next[x]=new node();
            p->next[x]->id=++idh;
            p->next[x]->ch1=s[i];

        }
        p=p->next[x];
    }
    p->word++;
    return;
}
/*void print1(node* p){
    for (int i=1;i<=26;++i) if (p->next[i]!=NULL) {
        printf("%d%d%c\n",p->next[i]->word,p->next[i]->id,p->next->ch1)
        print1(p->next[i]);
    }
}*/
node *d[10000];
void print1(node* p){
    int op=-1,cl=0;
    node *p1,*p2;
    d[cl]=p;
    while (opprintf("a:%d %d %c %d\n",p1->word,p1->id,p1->ch1,p1->ch2);
        for (int i=1;i<=26;++i){
            if (p1->next[i]!=NULL){
                p2=p1->next[i];
                printf("b:%d %d %c %d\n",p2->word,p2->id,p2->ch1,p2->ch2);
                d[++cl]=p2;
            }
        }
    }

}
node *data[550000];
void fail1(){
    int op=-1;
    int cl=0;
    data[cl]=root;
    while(op*p=data[++op];
        for(int i=1;i<=26;++i){
            if(p->next[i]!=NULL){
                ++cl;
                data[cl]=p->next[i];

            node *p1=p->fail;
            while(p1!=NULL&&p1->next[i]==NULL) p1=p1->fail;
            if(p1==NULL){
                p->next[i]->fail=root;
                p->next[i]->ch2=root->id;
            }else{
                p->next[i]->fail=p1->next[i];
                p->next[i]->ch2=p1->next[i]->id;
            }
            }
        }
    }
}
void query(char s[]){
    node *p=root;
    int l=strlen(s);
    //printf("%s",s);
    for(int i=0;iint x=s[i]-'a'+1;
        while(p!=NULL&&p->next[x]==NULL) p=p->fail;
        if (p==NULL){
            p=root;continue;
        }
        p=p->next[x];
        node *p1=p;
        while(p1!=NULL){
            if (p1->word!=0){
                ans+=p1->word;p1->word=0;
            }
            p1=p1->fail;
        }
    }
}
/*void fail1(){
    int op=-1,cl=0;
    data[cl]=root;
    while (op*p=data[++op];
        for (int i=1;i<=26;++i){
            if (p->next[i]!=NULL){

            }
        }

    }
}*/
int main(){
    freopen("hdu2222.in","r",stdin);
    //freopen("hdu2222.out","w",stdout);
    scanf("%d",&case1);
    while(case1--){
        idh=0;
        root = new node();
        //printf("%d",root->id);
        scanf("%d",&n);
        for (int i=1;i<=n;++i){
            scanf("%s",str1);    
            insert1(str1);
        }
        //print1(root);
        fail1();
        //print1(root);
        scanf("%s",str2);
        ans=0;
        query(str2);
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(AC自动机)