HDU2222 Keywords Search(AC自动机)

来源:互联网 发布:数控火焰切割编程代码 编辑:程序博客网 时间:2024/06/11 03:30

Problem Description

In the modern time, Search engine came into the life of everybody like
Google, Baidu, etc. Wiskey also wants to bring this feature to his
image retrieval system. Every image have a long description, when
users type some keywords to find the image, the system will match the
keywords with description of image and show the image which the most
keywords be matched. To simplify the problem, giving you a description
of image, and some keywords, you should tell me how many keywords will
be match.

Input

First line will contain one integer means how many cases will follow
by. Each case will contain two integers N means the number of keywords
and N keywords follow. (N <= 10000) Each keyword will only contains
characters ‘a’-‘z’, and the length will be not longer than 50. The
last line is the description, and the length will be not longer than
1000000.

Output

Print how many keywords are contained in the description.

Sample Input

15shehesayshrheryasherhs

Sample Output

3

思路

给你给了n个字符串,然后接下来有一个串,问在这个串中,那n个串出现过几次

AC自动机:Aho-Corasick
automation,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一。一个常见的例子就是给出n个单词,再给出一段包含m个字符的文章,让你找出有多少个单词在文章里出现过

也就是AC自动机的基本题型,关于AC自动机的介绍:
AC自动机

kuangbinAC自动机小结
采用kuangbin的模板,在建立fail指针的时候进行一个优化,把空节点变成根节点节省时间

第一次用我原本写字典树的风格超时了,也不知道为什么,可以当做模板,存一下
两份代码的思想是一样的,就是写的格式不一样

代码1(kuangbin风格)

#include <bits/stdc++.h>using namespace std;const int N=5e5+20;struct dicTree{    int next[N][26],fail[N],sum[N];    int root,sz;    int newnode()    {        for(int i=0; i<26; i++)            next[sz][i]=-1;        sum[sz++]=0;        return sz-1;    }    void init()    {        sz=0;        root=newnode();    }    void insert(char *s)    {        int len=strlen(s);        int now=root;        for(int i=0; i<len; i++)        {            int to=s[i]-'a';            if(next[now][to]==-1)                next[now][to]=newnode();            now=next[now][to];        }        sum[now]++;    }    void build()    {        queue<int>q;        fail[root]=root;        for(int i=0; i<26; i++)        {            if(next[root][i]==-1)                next[root][i]=root;            else            {                fail[next[root][i]]=root;                q.push(next[root][i]);            }        }        while(!q.empty())        {            int now=q.front();            q.pop();            for(int i=0; i<26; i++)            {                if(next[now][i]==-1)                    next[now][i]=next[fail[now]][i];                else                {                    fail[next[now][i]]=next[fail[now]][i];                    q.push(next[now][i]);                }            }        }    }    int query(char *s)    {        int len=strlen(s);        int now=root;        int res=0;        for(int i=0; i<len; i++)        {            int to=s[i]-'a';            now=next[now][to];            int temp=now;            while(temp!=root)            {                res+=sum[temp];                sum[temp]=0;                temp=fail[temp];            }        }        return res;    }} ac;char s[1000000+20];int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        ac.init();        while(n--)        {            scanf("%s",s);            ac.insert(s);        }        ac.build();        scanf("%s",s);        printf("%d\n",ac.query(s));    }}

代码2(我的风格。。TLE)

#include <bits/stdc++.h>using namespace std;const int N=5e5+20;struct dicTree{    struct node    {        int next[26];        int sum;        int fail;    } T[N];    int root,sz;    int newnode()    {        for(int i=0; i<26; i++)            T[sz].next[i]=-1;        T[sz].sum=0;        T[sz++].fail=0;        return sz-1;    }    void init()    {        sz=0;        root=newnode();    }    void insert(char *s)    {        int len=strlen(s);        int now=root;        for(int i=0; i<len; i++)        {            int to=s[i]-'a';            if(T[now].next[to]==-1)                T[now].next[to]=newnode();            now=T[now].next[to];        }        T[now].sum++;    }    void build()    {        queue<int>q;        T[root].fail=root;        for(int i=0; i<26; i++)        {            if(T[root].next[i]==-1)                T[root].next[i]=root;            else            {                T[T[root].next[i]].fail=root;                q.push(T[root].next[i]);            }        }        while(!q.empty())        {            int now=q.front();            q.pop();            for(int i=0; i<26; i++)            {                if(T[now].next[i]==-1)                    T[now].next[i]=T[T[now].fail].next[i];                else                {                    T[T[now].next[i]].fail=T[T[now].fail].next[i];                    q.push(T[now].next[i]);                }            }        }        return;    }    int query(char *s)    {        int len=strlen(s);        int now=root,temp;        int res=0;        for(int i=0; i<len; i++)        {            int to=s[i]-'a';            now=T[now].next[to];            temp=now;            while(temp!=root)            {                res+=T[temp].sum;                T[temp].sum=0;                temp=T[temp].fail;            }        }        return res;    }} ac;char s[1000000+20];int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        ac.init();        while(n--)        {            scanf("%s",s);            ac.insert(s);        }        ac.build();        scanf("%s",s);        printf("%d\n",ac.query(s));    }}