HDU 2222 Keywords Search(AC自动机)

本文介绍了一个基于AC自动机的问题实例,通过构建AC自动机解决关键词搜索问题,包括AC自动机的构建过程、关键词匹配算法及其实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Keywords Search

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


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
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:   2896  3065  2243  2825  3341 
 
分析:
就是多给模板串和一个文本串进行匹配,问你进行匹配成功的次数
也可以进行多次kmp,但是效率不高
KMP加字典树就是AC自动机
还不是很理解AC自动机
套的模板
 
ac自动机第一题!!!
加油!
code:
#include<queue>
#include<set>
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
/*
    ac自动机
*/
struct acnode
{
    int sum;
    acnode* next[26];
    acnode* fail;
    acnode()
    {
        for(int i =0; i<26; i++)
            next[i]=NULL;
        fail= NULL;
        sum=0;
    }
};
acnode *root;
int cnt;
acnode* newnode()
{
    acnode *p = new acnode;
    for(int i =0; i<26; i++)
        p->next[i]=NULL;
    p->fail = NULL;
    p->sum=0;
    return p;
}
//插入函数
void Insert(char *s)
{
    acnode *p = root;
    for(int i = 0; s[i]; i++)
    {
        int x = s[i] - 'a';
        if(p->next[x]==NULL)
        {
            acnode *nn=newnode();
            for(int j=0; j<26; j++)
                nn->next[j] = NULL;
            nn->sum = 0;
            nn->fail = NULL;
            p->next[x]=nn;
        }
        p = p->next[x];
    }
    p->sum++;
}
//获取fail指针,在插入结束之后使用
void getfail()
{
    queue<acnode*> q;
    for(int i = 0 ; i < 26 ; i ++ )
    {
        if(root->next[i]!=NULL)
        {
            root->next[i]->fail = root;
            q.push(root->next[i]);
        }
    }
    while(!q.empty())
    {
        acnode* tem = q.front();
        q.pop();
        for(int i = 0; i<26; i++)
        {
            if(tem->next[i]!=NULL)
            {
                acnode *p;
                if(tem == root)
                {
                    tem->next[i]->fail = root;
                }
                else
                {
                    p = tem->fail;
                    while(p!=NULL)
                    {
                        if(p->next[i]!=NULL)
                        {
                            tem->next[i]->fail = p->next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL)
                        tem->next[i]->fail = root;
                }
                q.push(tem->next[i]);
            }
        }
    }
}

//匹配函数

void ac_automation(char *ch)
{
    acnode *p = root;
    int len = strlen(ch);
    for(int i = 0; i < len; i++)
    {
        int x = ch[i] - 'a';
        while(p->next[x]==NULL && p != root)//没匹配到,那么就找fail指针。
            p = p->fail;
        p = p->next[x];
        if(!p)
            p = root;
        acnode *temp = p;
        while(temp != root)
        {
            if(temp->sum >= 0)
                /*
                在这里已经匹配成功了,执行想执行的操作即可,怎么改看题目需求+
                */
            {
                cnt += temp->sum;
                temp->sum = -1;
            }
            else break;
            temp = temp->fail;
        }
    }
}
int main()
{
    int t;
    int n;
    scanf("%d",&t);
    char c[1000005];
    while(t--)
    {
        cnt = 0;
        scanf("%d",&n);
        root = newnode();
        for(int i = 0 ; i < n; i++)
        {
            scanf("%s",c);
            Insert(c);
        }
        getfail();
        scanf("%s",c);
        ac_automation(c);
        printf("%d\n",cnt);
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/yinbiao/p/9458447.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值