hdu 3695 AC自动机模板题

本文介绍了一种基于AC自动机的关键词搜索算法实现方法,该方法能够高效地在大量文本中查找多个关键词。通过构建AC自动机并利用失配指针加速搜索过程,文章详细解释了算法的工作原理及其实现细节。

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

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23502    Accepted Submission(s): 7763


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 <iostream>
#include <queue>
#include <cstring>

using namespace std;

//子树节点是在插入时new的,
//寻找失配指针中使用的队列是直接调用STL的
const int kind = 26;
struct node
{
    node *fail;
    node *next[kind];
    int count;//记录当前前缀是完整单词出现的个数
    node()
    {
        fail = NULL;
        count = 0;
        memset(next,NULL,sizeof(next));
    }
};

void insert(char *str,node *root)
{
    node *p=root;
    int i=0,index;
    while(str[i])
    {
        index = str[i]-'a';
        if(p->next[index]==NULL) p->next[index]=new node();
        p=p->next[index];
        i++;
    }
    p->count++;

}

//寻找失败指针
void build_ac_automation(node *root)
{
    int i;
    queue<node *>Q;
    root->fail = NULL;
    Q.push(root);
    while(!Q.empty())
    {
        node *temp = Q.front();//q[head++];//取队首元素
        Q.pop();
        node *p = NULL;
        for(i=0; i<kind; i++)
        {
            if(temp->next[i]!=NULL)//寻找当前子树的失败指针
            {
                p = temp->fail;
                while(p!=NULL)
                {
                    if(p->next[i]!=NULL)//找到失败指针
                    {
                        temp->next[i]->fail = p->next[i];
                        break;
                    }
                    p = p->fail;
                }

                if(p==NULL)//无法获取,当前子树的失败指针为根
                    temp->next[i]->fail = root;

                Q.push(temp->next[i]);
            }
        }
    }
}


//询问str中包含n个关键字中多少种即匹配
int query(char *str,node *root)
{
    int i = 0,cnt = 0,index,len;
    len = strlen(str);
    node *p = root;
    while(str[i])
    {
        index = str[i]-'a';
        while(p->next[index]==NULL&&p!=root)//失配
            p=p->fail;
        p=p->next[index];
        if(p==NULL)//失配指针为根
            p = root;

        node *temp = p;
        while(temp!=root&&temp->count!=-1)//寻找到当前位置为止是否出现病毒关键字
        {
            //if(temp->count!=0)
            cnt+=temp->count;
            temp->count=-1;
            temp=temp->fail;
        }
        i++;
    }
    return cnt;
}

char str[1111111];
char words[1111111];
int T,n;

node* root;

int main()
{
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&n);
        root=new node();
        while (n--)
        {
            scanf("%s",words);
            insert(words,root);
        }
        build_ac_automation(root);
        scanf("%s",str);
        int ans=query(str,root);
        printf("%d\n",ans);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值