高级数据结构(五)ac自动机

本文介绍了AC自动机,一种用于多模式字符串匹配的高效算法,它在Trie的基础上通过构建fail指针避免回溯。文章提供了详细的AC自动机原理介绍,并附带了C++代码实现,包括节点定义、插入模式串、初始化和构建fail指针、匹配查询以及清除内存等关键函数。代码示例展示了如何构建和使用AC自动机进行字符串匹配。
摘要由CSDN通过智能技术生成

高级数据结构(五)ac自动机

前言

ac自动机是解决多模式字符串匹配的算法。KMP是单模式字符串匹配算法。Trie也可以解决多模式匹配的问题。但是Trie在匹配多个模式串时需要回溯。ac自动机就是在Trie的基础上,通过构建fail指针(失配指针)来避免回溯。

一、ac自动机

偷个懒,理解ac自动机请看这两篇文章
AC自动机算法
AC自动机讲解超详细

二、代码演示

//
// Created by lhx on 7/24/21.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>

#define base 26
#define str_len 100

typedef struct Node {
    char *str;
    struct Node *next[base], *fail;
} Node;

std::queue<Node*> que;

Node *getNewNode() {
    Node *p = (Node *)malloc(sizeof(Node));
    p->str = NULL;
    for (int i = 0; i < base; i++) {
        p->next[i] = NULL;
    }
    p->fail = NULL;
    return p;
}

char *copy_str(char *str) {
    char *s = (char *)malloc(sizeof(strlen(str)));
    for (int i = 0; str[i]; i++) {
        s[i] = str[i];
    }
    return s;
}

Node *insert(Node *root, char *str) {
    Node *p = root;
    for (int i = 0; str[i]; i++) {
        int ind = str[i] - 'a';
        if (p->next[ind] == NULL) p->next[ind] = getNewNode();
        p = p->next[ind];
    }
    p->str = copy_str(str);
    return root;
}

Node *init_build_fail(Node *root) {
    root->fail = NULL;
    for (int i = 0; i < base; i++) {
        if (root->next[i] == NULL) continue;
        root->next[i]->fail = root;
        que.push(root->next[i]);
    }
    return root;
}

Node *build_fail(Node *root) {
    init_build_fail(root);
    Node *p;
    while (!que.empty()) {
        p = que.front();
        que.pop();
        Node *k = p->fail;
        for (int i = 0; i < base; i++) {
           if (p->next[i] == NULL) continue;
           //for example, next[i] = a, if a != NULL, find fail pointer that has a
           while (k != root && k->next[i] == NULL) {
               k = k->fail;
           }
           //if find k that have a
           if (k->next[i] != NULL) k = k->next[i];
           //if don't find k , k = root
           p->next[i]->fail = k;
           que.push(p->next[i]);
        }
    }
}


void match(Node *root, char *str) {
    Node *p = root, *q;
    for (int i = 0; str[i]; i++) {
        int ind = str[i] - 'a';
        //如果当前节点下没有str[i],去找与当前节点后缀相同的节点。
        while (p->next[ind] == NULL && p != root) {
            p = p->fail;
        }
        if (p->next[ind]) p = p->next[ind];
        q = p;
        //如果匹配到了,那么输出下所有的后缀相同的节点。
        while (q) {
            if (q->str != NULL) {
                printf("%s\n",q->str);
            }
            q = q->fail;
        }
    }
}

void clear(Node *root) {
    if (root == NULL) return ;
    for (int i = 0; i < base; i++) {
        clear(root->next[i]);
    }
    free(root->str);
    free(root->fail);
    free(root);
    return ;
}


int main() {
    Node *root = getNewNode();
    int n;
    char buff[str_len];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%s", buff);
        root = insert(root, buff);
    }

    build_fail(root);
    while (scanf("%s", buff) != EOF) {
        match(root, buff);
    }
    clear(root);
    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值