Keywords Search(AC自动机入门题)

Keywords Search

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/131072K (Java/Other)

Total Submission(s) : 20   Accepted Submission(s) : 17

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自动机模板

AC代码:

#include<iostream>
#include<string>
#include<string.h>
#include<stdio.h>
#include<queue>
using namespace std;
struct Trie
{
    int values;
    Trie *child[26];
    Trie *fail;              
    Trie()
    {
        values=0;
        memset(child,NULL,sizeof(child));
        fail=NULL;
    }
}*root;
int ans;
char patten[60];
char text[1000010];
void create(char s[])                   //构建字典树
{
    Trie *x=root;
    for(int i=0;i<strlen(s);i++)
    {
        int d=s[i]-'a';
        if(x->child[d]==NULL)
        {
            x->child[d]=new Trie;
        }
        x=x->child[d];
    }
    x->values++;
}
void deleteTrie(Trie *x)                //清除字典树
{
    if(x==NULL)
        return;
    for(int i=0;i<26;i++)
    {
        if(x->child[i]!=NULL)
        {
            deleteTrie(x->child[i]);
        }
    }
    delete x;
}
void build_AC_automaton()               //构建AC自动机
{
    Trie *p;                            
    p=root;
    queue<Trie*> qu;                    //使用广搜实现
    qu.push(p);
    while(!qu.empty())
    {
        p=qu.front();                   
        qu.pop();
        for(int i=0;i<26;i++)
        {
            if(p->child[i]!=NULL)
            {
                if(p==root)
                {
                    p->child[i]->fail=root;
                }
                else
                {
                    Trie *node=p->fail;         
                    while(node!=NULL)       //查找当前节点的fail指针是否为空
                    {
                        if(node->child[i]!=NULL)    //判断当前节点和fail指针指向的孩子节点是否相等
                        {
                            p->child[i]->fail=node->child[i]; //相等令p的孩子节点fail指针指向p节点指向的fail指针的孩子节点
                            break;
                        }
                        node=node->fail;
                    }
                    if(node==NULL)
                        p->child[i]->fail=root;
                }
                qu.push(p->child[i]);
            }
        }
    }
}
void find_in_AC_automaton()         //查找过程
{
    Trie *p;
    p=root;
    int index=0;
    while(text[index]!='\0')
    {
        int id=text[index]-'a';
        while(p->child[id]==NULL && p!=root)
            p=p->fail;
        p=p->child[id];
        if(p==NULL) p=root;
        Trie *temp=p;
        while(temp!=NULL && temp->values!=-1)
        {
            ans+=temp->values;
            temp->values=-1;
            temp=temp->fail;
        }
        index++;
    }
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        root = new Trie;
        for(int i=0;i<n;i++)
        {
            scanf("%s",patten);
            create(patten);
        }
        scanf("%s",text);
        build_AC_automaton();
        ans=0;
        find_in_AC_automaton();
        printf("%d\n",ans);
        deleteTrie(root);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值