HDU2222 -ac自动机

Keywords Search

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


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
 


题目大意:

给n 个字母,以及一个串,问这个串中有多少个字母,不能重复计数


题目思路:

AC自动机模板,对于AC自动机的讲解可以看这篇博文:ac自动机详解


AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;

const int maxn = 1e6+10;
const int maxtot = 5e5+10;
int Size;

struct Tree
{
    int nex[26];
    int fail,cnt;
    void init()
    {
        fail = cnt = 0;
        memset(nex,0,sizeof(nex));
    }
}node[maxtot];

void Insert(char *s)
{
    int n  = strlen(s);
    int now = 0;
    for(int i=0;i<n;i++)
    {
        int id = s[i]-'a';
        if(!node[now].nex[id])
        {
            node[now].nex[id] = Size++;
        }
        now = node[now].nex[id];
    }
    node[now].cnt++;
}
queue<int>q;
void Build()
{
    node[0].fail = -1;
    q.push(0);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        for(int i=0;i<26;i++)
        {
            if(node[u].nex[i])
            {
                if(u==0)node[node[u].nex[i]].fail = 0;
                else
                {
                    int v = node[u].fail ;
                    while(v!=-1)
                    {
                        if(node[v].nex[i])
                        {
                            node[node[u].nex[i]].fail = node[v].nex[i];
                            break;
                        }
                        v = node[v].fail;
                    }
                    if(v==-1)node[node[u].nex[i]].fail = 0;
                }
                q.push(node[u].nex[i]);
            }
        }
    }
}

int Search(char *s)
{
    int n = strlen(s);
    int res = 0,now = 0;
    for(int i=0;i<n;i++)
    {
        int id = s[i]-'a';
        if(node[now].nex[id])now = node[now].nex[id];
        else
        {
            int p = node[now].fail;
            while(p!=-1&&node[p].nex[id]==0)p = node[p].fail;
            if(p==-1)now = 0;
            else now = node[p].nex[id];
        }

        if(node[now].cnt)
        {
            int uu = now;
            while(uu)
            {
                res+=node[uu].cnt;
                node[uu].cnt = 0;
                uu = node[uu].fail;
            }
        }

    }

    return res;
}

void init()
{

    for(int i=0;i<maxtot;i++)
        node[i].init();
    Size = 1;
}
char s[maxn];
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n;
        scanf("%d",&n);
        init();
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            Insert(s);
        }
        Build();
        scanf("%s",s);
        printf("%d\n",Search(s));

    }
    return 0;
}





全部评论

相关推荐

昨天 13:47
门头沟学院 Java
投递小米集团等公司10个岗位
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
07-11 15:12
门头沟学院 Java
别人在上班,我就在工位上看看视频啥的,这正常吗?
程序员小白条:实习就是摸鱼,只是公司指标,把你进来了,可能那时候客户很多,但等你进来的时候,已经是淡季了,根本没多少需求,或者说根本不适合实习生去完成,因此你就每天干坐着就行,可能1,2个月都没需求
实习生的蛐蛐区
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务