Keywords Search HDU - 2222(AC自动机模板)

本文介绍了一个AC自动机的实际应用案例,通过构建AC自动机来高效地在长字符串中搜索多个关键词。文章详细解释了AC自动机的工作原理,并提供了一段完整的C++代码实现。

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

Keywords Search HDU - 2222

 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自动机讲解1

AC自动机讲解2

code:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000010;
int cnt;
struct node{
    node *Next[26];
    node *fail;
    int sum;
};//字典树节点

node *root;
char key[70];//单词
node *q[maxn];//模拟队列
int head,tail;
node *newnode;
char pattern[maxn];//模式串
int n;

void Insert(char *s){//建立字典树
    node *p = root;
    for(int i = 0; s[i]; i++){
        int x = s[i] - 'a';
        if(p->Next[x] == NULL){//还没有这个节点新建一个
            newnode = (node*)malloc(sizeof(node));
            for(int j = 0; j < 26; j++){//初始化
                newnode->Next[j] = NULL;
            }
            newnode->fail = NULL;
            newnode->sum = 0;
            p->Next[x] = newnode;
        }
        p = p->Next[x];//走到下一个节点
    }
    p->sum++;//以最后一个字母为结尾的单词个数加一
}

void getFail(){//得到fail指针,形成ac自动机
    head = 0;
    tail = 0;
    q[tail++] = root;
    node *p;
    node *tmp;
    while(head < tail){
        tmp = q[head++];
        for(int i = 0; i < 26; i++){
            if(tmp->Next[i]){//如果当前节点的下一个节点有i这个字母,下面我们要确定当前节点下一个节点的fail指针指向哪里
                if(tmp == root){
                    tmp->Next[i]->fail = root;//下一个节点fail指针直接指向根节点
                }
                else{
                    p = tmp->fail;//否则先找到当前节点的fail指向的节点
                    while(p){
                        if(p->Next[i]){//如果当前节点的fail指向的节点下一个有我们需要在字符,就让当前节点下一个节点的fail指向它
                            tmp->Next[i]->fail = p->Next[i];
                            break;
                        }
                        p = p->fail;
                    }
                    if(p == NULL) tmp->Next[i]->fail = root;//如果找不到符合的就直接指回根,相当于要从头开始
                }
                q[tail++] = tmp->Next[i];//下一个节点入队
            }
        }
    }
}

void ac_Actomation(char *ch){
    node *p = root;
    int len = strlen(ch);
    for(int i = 0; i < len; i++){
        int x = ch[i] - 'a';
        while(!p->Next[x] && p != root) p = p->fail;//如果进入这个循环说明失配,即当前节点下一个没有字符ch[i],所以找fail
        p = p->Next[x];//从fail下一个开始
        if(!p) p = root;//如果是空的就说明确实没有以当前字符为结尾的单词,让他等于根
        node *tmp = p;
        while(tmp != root){//若不为根(说明能够找到以当前字符为结尾的单词)
            if(tmp->sum >= 0){
                cnt += tmp->sum;
                tmp->sum = -1;
            }
            else{
                break;//没有就终止,因为如果这个都没有,它的fail也不可能有
            }
            tmp = tmp->fail;
        }
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        root = (node*)malloc(sizeof(node));
        for(int j = 0; j < 26; j++){
            root->Next[j] = NULL;
        }
        root->fail = NULL;
        root->sum = 0;
        scanf("%d",&n);
        for(int i = 0; i < n; i++){
            scanf("%s",key);
            Insert(key);
        }
        scanf("%s",pattern);
        cnt = 0;
        getFail();
        ac_Actomation(pattern);
        printf("%d\n",cnt);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值