HDU2222 Keywords Search (AC自动机)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2222

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个字符串中的。

题解:

ac自动机入门,一直觉得挺难的东西,但学完后感觉并不难,和KMP一个道理的。不了解ac自动机的可以先去找一些资料视频等学习一下。然后说的就是MLE很头疼,看了kuangbin的解法可以用数组模拟指针会好很多不然很难受啊。下面这个题是用指针做的,卡着时间了,后一篇博客会给出指针MLE到死后用数组模拟过的。

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

using namespace std;
typedef long long LL;
struct node
{
    int cnt;
    node* next[27];
    node* fail;
    node()
    {
        cnt=0;
        fail = NULL;
        for(int i=0;i<26;i++)
            next[i]=NULL;
    }
};
node *q[505000];
char s[1100000];
node* root;
int top;
void build(char *s)
{
    node* p=root;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int x = s[i]-'a';
        if(p->next[x]==NULL)
            p->next[x]= new node;
        p=p->next[x];
    }
    p->cnt++;
}
void getfail()
{
    int in,out;
    in=out=0;
    q[in++]=root;
    while(in>out)
    {
        node* p =q[out++];
        for(int i=0;i<26;i++)
        {
            if(p->next[i]==NULL)continue;
            q[in++]=p->next[i];
            node* t =  p->fail;
            while(t)
            {
                if(t->next[i]!=NULL)
                {
                    p->next[i]->fail=t->next[i];
                    break;
                }
                t = t->fail;
            }
            if(t==NULL)
            {
                p->next[i]->fail=root;
            }
        }
    }
}
int work(char *s)
{
    int ans=0;
    int len=strlen(s);
    node* p = root;
    for(int i=0;i<len;i++)
    {
        int x = s[i]-'a';
        while(p!=NULL&&p->next[x]==NULL)
            p=p->fail;
        if(p==NULL)
        {
            p=root;
            continue;
        }
        node* t = p->next[x];
        while(t!=NULL&&t->cnt!=-1)
        {
            if(t->cnt!=0)
            {
                ans+=t->cnt;
                t->cnt=-1;
            }
            t=t->fail;
        }
        p=p->next[x];
    }
    return ans;
}
void del(node* root)
{
    for(int i=0;i<26;i++)
    {
        if(root!=NULL)del(root->next[i]);
    }
    delete root;
}
int main()
{
    int n,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        top=0;
        root = new node;
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            build(s);
        }
        getfail();
        scanf("%s",s);
        printf("%d\n",work(s));
        del(root);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值