HDU 2222 Keywords Search

本文介绍了一种使用AC自动机实现的高效关键词检索方法,适用于图像描述等文本中的关键词匹配任务。通过构建AC自动机并利用其进行模式匹配,可以快速找出文本中包含的所有关键词。

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

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



she 
he 
say 
shr 
her 
yasherhs

Sample Output

3

题目大意:

输入n表示接下来有n组测试数据,输入m表示接下来有m个单词,最后输入一个字符串。问字符串中含有多少个前面出现的单词。

C++

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
const int allSon=26;
char patten[60];
char text[1000010];
int ans;
struct TrieNode
{
    struct TrieNode *son[allSon];
    struct TrieNode *fail;
    int num;
}*root;
TrieNode* createNode()
{
    TrieNode *p;
    p = (TrieNode*)malloc(sizeof(TrieNode));
    for(int i = 0; i < allSon; i++) p->son[i] = NULL;
    p->num = 0;
    p->fail = NULL;
    return p;
}
void insertPatten()
{
    TrieNode *p;
    p = root;
    int index = 0;
    while(patten[index] != '\0')
    {
        int lowercase = patten[index]-'a';
        if(p->son[lowercase]==NULL)
        {
            p->son[lowercase] = createNode();
        }
        p = p->son[lowercase];
        index++;
    }
    p->num++;
}
void build_AC_automaton()
{
    TrieNode *p;
    p = root;
    queue<TrieNode*>qu;
    qu.push(p);
    while(!qu.empty())
    {
        p = qu.front();
        qu.pop();
        for(int i = 0; i < allSon; i++)
        {
            if(p->son[i] != NULL)
            {
                if(p == root)
                {
                    p->son[i]->fail = root;
                }
                else
                {
                    TrieNode *node = p->fail;
                    while(node != NULL)
                    {
                        if(node->son[i]!=NULL)
                        {
                            p->son[i]->fail = node->son[i];
                            break;
                        }
                        node = node->fail;
                    }
                    if(node == NULL)
                        p->son[i]->fail = root;
                }
                qu.push(p->son[i]);
            }
        }
    }
}
void find_in_AC_automaton()
{
    TrieNode *p;
    p = root;
    int index = 0;
    while(text[index] != '\0')
    {
        int lowercase = text[index]-'a';
        while(p->son[lowercase]==NULL && p!=root)
            p = p->fail;
        p = p->son[lowercase];
        if(p == NULL) p = root;
        TrieNode *temp = p;
        while(temp!=NULL && temp->num!=-1)
        {
            ans += temp->num;
            temp->num = -1;
            temp = temp->fail;
        }
        index++;
    }
}
void freeNode(TrieNode *node)
{
    if(node != NULL)
    {
        for(int i = 0; i < allSon; i++)
            freeNode(node->son[i]);
    }
    free(node);
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        root = createNode();
        for(int i = 0; i < n; i++)
        {
            scanf("%s",patten);
            insertPatten();
        }
        scanf("%s",text);
        build_AC_automaton();
        ans = 0;
        find_in_AC_automaton();
        printf("%d\n",ans);
        freeNode(root);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值