HDU2222:Keywords Search

来源:互联网 发布:linux rpm命令详解 编辑:程序博客网 时间:2024/05/22 00:49

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
1
5
she
he
say
shr
her
yasherhs

Sample Output
3

题目传送门
牧哥翻译:有n(n<=10000)个单词(长度不超过50,保证都为小写字母)和一句话(长度不超过1000000) ,求出这句话包括几个单词

AC自动机。
由KMP,字典树组成。
构成:
1.构造一棵Trie,作为AC自动机的搜索数据结构。
2.构造fail指针,使当前字符失配时跳转到具有最长公共前后缀的字符继续匹配。如同 KMP算法一样, AC自动机在匹配时如果当前字符匹配失败,那么利用fail指针进行跳转。由此可知如果跳转,跳转后的串的前缀,必为跳转前的模式串的后缀并且跳转的新位置的深度(匹配字符个数)一定小于跳之前的节点。所以我们可以利用 bfs在 Trie上面进行 fail指针的求解。
3.扫描主串进行匹配。

代码如下:

#include<queue>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>using namespace std;struct node{    int s,c[27],fail;    node()    {        s=fail=0;        memset(c,-1,sizeof(c));    }}t[510000];int tot,ans,n;char a[11000000];void clean(int x){    t[x].s=t[x].fail=0;    for(int i=1;i<=26;i++)t[x].c[i]=-1;}void bt(int root){    int x=root,len=strlen(a+1);    for(int i=1;i<=len;i++)    {        int y=a[i]-'a'+1;        if(t[x].c[y]==-1)        {            t[x].c[y]=++tot;            clean(tot);        }        x=t[x].c[y];    }    t[x].s++;}queue<int > q;void bfs(){    int x;    q.push(0);    while(q.empty()==0)    {        x=q.front();        for(int i=1;i<=26;i++)        {            int son=t[x].c[i];            if(son==-1)continue;            if(x==0)t[son].fail=0;            else            {                int j=t[x].fail;                while(j!=0&&t[j].c[i]==-1)j=t[j].fail;                t[son].fail=max(t[j].c[i],0);            }            q.push(son);        }        q.pop();    }}void solve(){    int x=0,len=strlen(a+1);    for(int i=1;i<=len;i++)    {        int y=a[i]-'a'+1;        while(x!=0&&t[x].c[y]==-1)x=t[x].fail;        x=t[x].c[y];        if(x==-1){x=0;continue;}        int j=x;        while(t[j].s!=0)        {            ans+=t[j].s;            t[j].s=0;            j=t[j].fail;        }    }    printf("%d\n",ans);}int main(){    int T;    scanf("%d",&T);    while(T--)    {        ans=tot=0;        scanf("%d",&n);        clean(0);        for(int i=1;i<=n;i++)        {            scanf("%s",a+1);            bt(0);        }        bfs();        scanf("%s",a+1);        solve();    }}

by_lmy

原创粉丝点击