Keywords Search 【AC自动机模板题】

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


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
 

#include<cstdio>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef __int64 ll;
const int N=26;
const int M=1000005;
const int L=52;

typedef struct Node
{
    int count;
    Node *Next[N];
    Node *fail;
    Node *last;
}Node;

Node * build()//建立节点
{
    Node *node=(Node *)malloc(sizeof(Node));
    node->count=0;
    node->fail=NULL;
    node->last=NULL;
    memset(node->Next,NULL,sizeof(node->Next));
    return node;
}

void actire_insert(Node *root,char str[])//日常插入
{
    Node *p=root;
    int id,i,len=strlen(str);
    for(i=0;i<len;i++)
    {
        id=str[i]-'a';
        if(p->Next[id]==NULL)
            p->Next[id]=build();
        p=p->Next[id];
    }
    p->count++;
}

//zi
void get_fail(Node *root)
{
    queue<Node *>q;
    q.push(root);//先将root入队
    int i;
    while(!q.empty())
    {
        Node *p=q.front();
        q.pop();
        for(i=0;i<N;i++)
        {//根表示弹出队列的 p
            if(p->Next[i]!=NULL)//找到存在相应的根的节点
            {
                if(p==root) p->Next[i]->fail=root;//此时如果根节点是root,就让根的节点的失败指针指向root
                else//否则
                {
                    Node *temp=p->fail;
                    //定义临时指针等于当前根节点的失败指针,这里是因为要找有最长公共前后缀继续匹配;
                    while(temp!=NULL)
                    {
                        if(temp->Next[i]!=NULL)
                        {
                            p->Next[i]->fail=temp->Next[i];
                            break;
                        }
                        if(temp->Next[i]==NULL)
                            temp=temp->fail;//如果根节点的失败指针没有与之相同的,继续循环,直到temp等于NULL
                    }
                    if(temp==NULL)
                        p->Next[i]->fail=root;
                }
                q.push(p->Next[i]);
            }
        }
    }
}

int found(Node *root,char str[])
{
    int id,len=strlen(str),ans=0;
    Node *p=root;
    for(int i=0;i<len;i++)
    {
        id=str[i]-'a';
        while(p->Next[id]==NULL&&p!=root)
            p=p->fail;
        p=p->Next[id];
        if(p==NULL)
            p=root;
        Node *temp=p;
        while(temp!=root&&temp->count!=-1)
        {
            ans+=temp->count;
            temp->count=-1;
            temp=temp->fail;
        }
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        Node *root=build();
        int n,i;
        scanf("%d",&n);
        char s[L],str[M];
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            actire_insert(root,s);
        }
        get_fail(root);
        scanf("%s",str);
        printf("%d\n",found(root,str));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值