前言
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;
}