Keywords Search AC自动机

本文介绍了一种使用AC自动机优化图像检索系统的算法,通过关键字匹配图像描述,实现高效搜索。文章详细阐述了AC自动机的构建过程及如何进行模式匹配,包括插入关键字、构建失败指针和查询关键字在描述中出现的次数。

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

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. 

InputFirst 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. 
OutputPrint how many keywords are contained in the description.Sample Input

1
5
she
he
say
shr
her
yasherhs

Sample Output

3
AC自动机:利用ttie树节省空间,利用kmp相似的原理构建fail指针进行匹配
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 26
#define L 31
#define INF 1000000009
#define eps 0.00000001
struct node
{
    node()
    {
        cnt = 0;
        fail = NULL;
        for (int i = 0; i < MAXN; i++)
            next[i] = NULL;
    }
    int cnt;
    struct node* next[MAXN];
    struct node* fail;
};
typedef struct node* Tree;
void insert(Tree T, const char* str)
{
    int p = 0;
    Tree tmp = T;
    while (str[p] != '\0')
    {
        int k = str[p] - 'a';
        if (tmp->next[k] == NULL)
        {
            tmp->next[k] = new node();
        }
        tmp = tmp->next[k];
        p++;
    }
    tmp->cnt++;
}
void build_ac(Tree root)
{
    root->fail = NULL;
    queue<Tree> q;
    q.push(root);
    while (!q.empty())
    {
        Tree tmp = q.front();
        q.pop();
        Tree p = NULL;
        for (int i = 0; i < MAXN; i++)
        {
            if (tmp->next[i] != NULL)
            {
                if (tmp == root)
                    tmp->next[i]->fail = root;
                else
                {
                    p = tmp->fail;
                    while (p != NULL)
                    {
                        if (p->next[i] != NULL)
                        {
                            tmp->next[i]->fail = p->next[i];
                            break;
                        }
                        p = p->fail;
                    }
                    if (p == NULL) tmp->next[i]->fail = root;
                }
                q.push(tmp->next[i]);
            }
        }
    }
}
int query(Tree root,const char* str)
{
    int i = 0;
    int count = 0;
    int l = strlen(str);
    Tree tmp = root;
    int index;
    while (str[i])
    {
        index = str[i] - 'a';
        while (tmp->next[index] == NULL && tmp != root)
            tmp = tmp->fail;
        tmp = tmp->next[index];
        if (tmp == NULL) tmp = root;
        Tree p = tmp;
        while (p != root)
        {
            count += p->cnt;
            p->cnt = 0;
            p = p->fail;
        }
        i++;
    }
    return count;
}

void del(Tree root)
{
    for (int i = 0; i < MAXN; i++)
        if (root->next[i] != NULL)
            del(root->next[i]);
    delete root;
}
char key[60], s[1000010];
int main()
{
    int T, n;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        Tree root = new node();
        root->cnt = 0;
        root->fail = NULL;
        for (int i = 0; i < MAXN; i++)
            root->next[i] = NULL;
        for (int i = 0; i < n; i++)
        {
            scanf("%s", key);
            insert(root, key);
        }
        build_ac(root);
        scanf("%s", s);
        printf("%d\n", query(root, s));
        del(root);
    }
}

 

转载于:https://www.cnblogs.com/joeylee97/p/7349061.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值