【HDU2222】Keywords Search——AC自动机基础

Keywords Search

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

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

Author
Wiskey
AC自动机讲解

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

const int Max =10000;

typedef struct node
{
    int cnt;
    node *next[26];
    node *fail;
    node()
    {
        fail = NULL; cnt = 0;

        for(int i = 0;i<26;i++) next[i] = NULL;
    }
}Tree;

int n,top;

char str[1000010];

void BuildTree(Tree * Root)
{
    int len = strlen(str);

    for(int i = 0 ;i<len;i++)
    {
        int ans = str[i]-'a';

        if(Root->next[ans]==NULL)
            Root->next[ans] = new node();

        Root= Root->next[ans];
    }

    Root->cnt++;

}

void BFS(Tree * Root)//构建fail
{

    Tree *u,*v;

    queue<Tree *>Q;

    Q.push(Root);

    while(!Q.empty())
    {
        u = Q.front();

        Q.pop();

        v = NULL;

        for(int i = 0;i<26;i++)
        {
            if(u->next[i]!=NULL)
            {
                if(u==Root)//根节点的孩子指向根节点
                {
                    u->next[i]->fail = Root;
                }
                else
                {
                    v = u->fail;

                    while(v!=NULL)//向前找最长的后缀
                    {
                        if(v->next[i]!=NULL)
                        {
                            u->next[i]->fail = v->next[i];

                            break;
                        }
                        v =v->fail;
                    }

                    if(v==NULL)//如果找不到则从头匹配。
                    {
                        u->next[i]->fail = Root;
                    }
                }

                Q.push(u->next[i]);
            }
        }
    }
}

int Query(Tree *Root)
{
    int len = strlen(str);

    int ans = 0;

    Tree * p = Root;

    for(int i = 0;i<len;i++)
    {
        int ant = str[i]-'a';

        while(p->next[ant]==NULL&&p!=Root) p = p->fail;// 不匹配时,通过跳转

        p=p->next[ant];

        if(p==NULL) p =Root;

        Tree *temp = p;

        while(temp!=Root&&temp->cnt!=-1)//统计数量
        {
            ans+=temp->cnt;

            temp->cnt = -1;

            temp = temp ->fail;
        }
    }

    return ans;
}

int main()
{
    int T;

    scanf("%d",&T);

    Tree * Root;

    while(T--)
    {
        scanf("%d",&n);

        Root = new node();

        for(int i = 0 ;i<n;i++)
        {
            scanf("%s",str);

            BuildTree(Root);
        }

        BFS(Root);

        scanf("%s",str);

        printf("%d\n",Query(Root));

    }
    return 0;
}

你可能感兴趣的:(【HDU2222】Keywords Search——AC自动机基础)