HDU 2222 Keywords Search(ac自动机)

来源:互联网 发布:视频解析源码 编辑:程序博客网 时间:2024/06/05 08:45

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

ac自动机裸题

code :

#include <iostream>#include <bits/stdc++.h>using namespace std;//const int maxn = 1e6+10;char str[1000];char text[1000010];struct node{    int t;    node * child[26];    node *fail;    node()    {        t = 0;        for (int i=0; i<26; i++)        {            child[i] = NULL;        }        fail =NULL;    }};node *root;void build()   //建立字典树{    //cout<<a<<endl;    node *p;    node * newnode;    int i;    p = root;    for (i=0; i<strlen(str); i++)    {        int m = str[i]-'a';        if (p->child[m]==NULL)        {            newnode = new node;            p->child[m] = newnode;        }        p = p->child[m];    }    p->t++;}void getfail()   //建立fail指针{    int i;    node *p;    p = root;    queue<struct node *>qu;    qu.push(p);    while (!qu.empty())    {        p = qu.front();        qu.pop();        for (i=0; i<26; i++)        {            if (p->child[i]!=NULL)            {                if (p==root)                {                    p->child[i]->fail = root;                }                else                {                    node * newnode= p->fail;                    while (newnode!=NULL)                    {                        if (newnode->child[i]!=NULL)                        {                            p->child[i]->fail = newnode->child[i];                            break;                        }                        newnode = newnode->fail;                    }                    if (newnode == NULL)                    {                        p->child[i]->fail = root;                    }                }                qu.push(p->child[i]);            }        }    }}int find_ac(){    int ans=0;    node *p;    p = root;    int i=0;    while (text[i]!='\0')    {        int m = text[i]-'a';        while (p->child[m]==NULL&&p!=root)        {            p = p->fail;        }        p = p->child[m];        if (p==NULL)            p = root;        node *newnode= p;        while (newnode!=NULL&&newnode->t!=-1)        {            ans += newnode->t;            newnode->t = -1;            newnode = newnode->fail;        }        i++;    }    return ans;}void ended(node *head)   //删除字典树{    for (int i=0; i<26; i++)    {        if (head->child[i]!=NULL)            ended(head->child[i]);    }    delete head;}int main(){    int N;    scanf("%d",&N);    while (N--)    {        //ans = 0;        int n;        scanf("%d",&n);        root =new node;        while (n--)        {            scanf("%s",str);            build();        }        scanf("%s",text);        getfail();        int ans = find_ac();        printf("%d\n",ans);        ended(root);    }    return 0;}