在计算机科学中,AC自动机(Aho-Corasick Automaton)是一种高效的多模式字符串匹配算法。它由 Alfred V. Aho 和 Margaret J. Corasick 在 1975 年提出,能够在一次扫描文本的过程中同时匹配多个关键词。本文将用通俗易懂的方式,手把手教你如何用 Python实现AC自动机,即使是编程小白也能轻松掌握。
想象一下,你有一个很长的文本(比如一篇新闻),需要快速找出其中是否包含“苹果”、“香蕉”、“橙子”等多个关键词。如果对每个关键词都单独做一次查找,效率会非常低。而 AC自动机 就像一个聪明的“词典机器人”,它先构建一个特殊的树结构(Trie + 失败指针),然后只需遍历一次文本,就能同时检测所有关键词是否存在。

我们将分三步实现:
class TrieNode: def __init__(self): self.children = {} # 子节点字典 self.fail = None # 失败指针 self.word_end = None # 如果是关键词结尾,存储关键词 self.output = [] # 输出列表(可存储多个关键词)from collections import dequeclass ACAutomaton: def __init__(self): self.root = TrieNode() def add_word(self, word): """向Trie树中添加一个关键词""" node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.word_end = word node.output.append(word) def build_failures(self): """使用BFS构建失败指针""" queue = deque() # 初始化:根节点的所有直接子节点的fail指向root for child in self.root.children.values(): child.fail = self.root queue.append(child) # BFS遍历 while queue: current_node = queue.popleft() for char, child in current_node.children.items(): # 找到当前节点的fail节点 fail_node = current_node.fail # 沿着fail链向上找,直到找到有char子节点的节点或回到root while fail_node and char not in fail_node.children: fail_node = fail_node.fail # 设置child的fail指针 child.fail = fail_node.children[char] if fail_node and char in fail_node.children else self.root # 合并output:把fail节点的output也加入当前节点 child.output += child.fail.output queue.append(child) def search(self, text): """在文本中搜索所有关键词,返回匹配结果列表""" node = self.root results = [] for i, char in enumerate(text): # 如果当前节点没有char子节点,沿fail指针回退 while node and char not in node.children: node = node.fail if node is None: node = self.root else: node = node.children[char] # 检查当前节点是否有匹配的关键词 if node.output: for word in node.output: start_index = i - len(word) + 1 results.append((start_index, word)) return results# 创建AC自动机ac = ACAutomaton()# 添加关键词keywords = ["he", "she", "his", "hers"]for word in keywords: ac.add_word(word)# 构建失败指针ac.build_failures()# 搜索文本text = "ushers"matches = ac.search(text)print("匹配结果:")for pos, word in matches: print(f"位置 {pos}: '{word}'")# 输出:# 位置 1: 'she'# 位置 2: 'he'# 位置 3: 'hers'AC自动机广泛应用于:
通过本教程,我们学习了 AC自动机 的基本原理和 Python实现AC自动机 的完整过程。它是一种强大的 多模式匹配 工具,特别适合需要同时查找多个关键词的场景。掌握这一算法,不仅能提升你的编程能力,还能解决实际工程中的性能瓶颈问题。
希望这篇关于 字符串匹配算法 的入门教程对你有所帮助!动手试试吧,你会发现AC自动机其实并不难。
本文由主机测评网于2025-12-18发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025129317.html