hdu 2896 病毒侵袭 (ac自动机)

来源:互联网 发布:淘宝网实拍保护入口 编辑:程序博客网 时间:2024/05/21 10:10

思路分析:

query 的时候不要把count 置为 0

坑就是他不只有字母= =  


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int max_next = 130;struct trie{    struct trie *fail;    struct trie *next[max_next];    int count;};trie *Q[500005];int head,tail;char str[1000005];void insert(trie *root,char *word,int id){    trie *p=root;    int i=0;    while(word[i]!='\0')    {        if(p->next[word[i]-' ']==NULL)        {            trie *temp = new trie;            for(int j=0;j<max_next;j++)temp->next[j]=NULL;            temp->count=0;            temp->fail=NULL;            p->next[word[i]-' ']=temp;        }        p=p->next[word[i]-' '];        i++;    }    p->count=id;}void acbuild(trie *root){    root->fail=NULL;    head=tail=0;    Q[tail++]=root;    while(head<tail)    {        trie *temp=Q[head++];        trie *p=NULL;        for(int i=0;i<max_next;i++)        {            if(temp->next[i]!=NULL)            {                if(temp==root)temp->next[i]->fail=root;                else                {                    p=temp->fail;                    while(p!=NULL)                    {                        if(p->next[i]!=NULL)                        {                            temp->next[i]->fail=p->next[i];                            break;                        }                        p=p->fail;                    }                    if(p==NULL)temp->next[i]->fail=root;                }                Q[tail++]=temp->next[i];            }        }    }}int ans[1111111];int top;bool vis[111111];bool query(trie *root){    int i=0;    bool cnt=0;    int len=strlen(str);    trie *p=root;    int index;    while(str[i])    {        int index = str[i]-' ';        while(p->next[index]==NULL && p!=root)        p=p->fail;        p=p->next[index];        if(p==NULL)p=root;        trie *temp=p;        while(temp!=NULL)        {            if(temp->count)ans[top++]=temp->count;            cnt=true;            //cnt+=temp->count;            //temp->count=0;            temp=temp->fail;        }        i++;    }    return cnt;}void del(trie *root){    for(int i=0;i<max_next;i++)    if(root->next[i])del(root->next[i]);    free(root);}char word[555];int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        trie *root = new trie;        for(int i=0;i<max_next;i++)root->next[i]=NULL;        root->count=0;        root->fail=NULL;        for(int i=1;i<=n;i++)        {            scanf("%s",word);            insert(root,word,i);       }       acbuild(root);       int m;       scanf("%d",&m);       int fans=0;       for(int cas=1;cas<=m;cas++)       {           top=0;           scanf("%s",str);           query(root);           fans+=(top==0?0:1);           if(top){               sort(ans,ans+top);               top=unique(ans,ans+top)-ans;               printf("web %d:",cas);               for(int i=0;i<top;i++)printf(" %d",ans[i]);               puts("");           }       }       printf("total: %d\n",fans);       //del(root);    }    return 0;}/*3aaabbbccc2bbbaaccaaabbbcccbbb*/


0 0
原创粉丝点击