HDU 2222 Keywords Search (AC自动机模板)

来源:互联网 发布:graphic软件 编辑:程序博客网 时间:2024/06/05 18:04

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 44439    Accepted Submission(s): 13990


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
一道入门模板题,最后一个是目标串,问其中出现了多少个模式串
#include <iostream>#include <cstdio>#include <cstring>#include <stdlib.h>#include <algorithm>#include <queue>#define N 500010using namespace std;struct Trie{    int next[N][26],fail[N],end[N];    int root , b;    int newnode()    {        for(int i=0;i<26;i++)            next[b][i]=-1;        end[b++]=0;        return b-1;    }    void init()    {        b=0;        root = newnode();    }    void insert(char s[])     //插入模式串    {        int len = strlen(s);        int now = root;        for(int i=0;i<len;i++)        {            if(next[now][s[i]-'a']==-1)                next[now][s[i]-'a'] = newnode();            now  = next[now][s[i]-'a'];        }        end[now]++;    }    void build()    //构建fail    {        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]);                }            }        }    }    void query(char s[])     //目标串s进行匹配    {        int len=strlen(s);        int now=root;        int ans=0;        for(int i=0;i<len;i++)        {            now = next[now][s[i]-'a'];            int temp = now;            while(temp != root)            {                ans += end[temp];                end[temp]=0;                temp=fail[temp];            }        }        printf("%d\n",ans);    }};char str[N*2]; Trie ac;int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n;        scanf("%d",&n);        ac.init();        for(int i=0;i<n;i++)        {            char s[51];            scanf("%s",s);            ac.insert(s);        }        ac.build();        scanf("%s",str);        ac.query(str);    }    return 0;}


0 0