# 博耶–摩尔（Boyer–Moore）字符串搜索算法

**博耶–摩尔字符串搜索算法**是一种精确字符串匹配算法：它从模式串的最后一个字符开始，自右向左地将模式串与文本比对，从而在较长的文本中找出模式串出现的位置，并借助预先计算好的移位表跳过文本中的大段内容。斯坦福研究所（Stanford Research Institute）的罗伯特·S·博耶（Robert S. Boyer）与施乐帕洛阿尔托研究中心（Xerox Palo Alto Research Center）的[J·斯特罗瑟·摩尔](https://www.edgepedia.org/zh/j-strother-moore)（J Strother Moore）于1975年前后发明了该算法，并于1977年10月在《ACM通讯》（*Communications of the ACM*）上发表<sup>[1](https://www.cin.ufpe.br/~paguso/courses/if767/bib/Boyer_1977.pdf)</sup><sup> • </sup><sup>[2](https://www.cs.utexas.edu/~moore/best-ideas/string-searching/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 原始发表 | 《一种快速的字符串搜索算法》（"A Fast String Searching Algorithm"），《ACM通讯》20(10)，第762–772页，1977年10月；DOI 10.1145/359842.359859<sup>[1](https://www.cin.ufpe.br/~paguso/courses/if767/bib/Boyer_1977.pdf)</sup><sup> • </sup><sup>[3](https://cir.nii.ac.jp/crid/1361699995558729216?lang=en)</sup> |
| 比较方向 | 从最后一个字符开始匹配模式串的字符，自右向左移动<sup>[1](https://www.cin.ufpe.br/~paguso/courses/if767/bib/Boyer_1977.pdf)</sup> |
| 移位规则 | 失配时，模式串按坏字符规则与好后缀规则所允许的最大位移移动<sup>[4](https://www.cs.tufts.edu/comp/150GEN/classpages/BoyerMoore.html)</sup> |
| 平均表现 | 对于长度为5的随机英文模式串，算法在位置 i 处找到匹配之前通常只检查文本的 i/4 个字符；平均执行的机器指令少于 i + patlen 条<sup>[1](https://www.cin.ufpe.br/~paguso/courses/if767/bib/Boyer_1977.pdf)</sup> |
| 最坏情况 | 约需 3n 次字符比较，由科尔（Cole）证明；加利利（Galil）1979年的修改使最坏情况变为线性，即使模式串多次出现也是如此<sup>[5](https://ir.cwi.nl/pub/5367/5367D.pdf)</sup> |
| 小字母表 | 在 DNA 这类子串频繁重现的字母表上，跳跃距离不再随模式串长度增长<sup>[2](https://www.cs.utexas.edu/~moore/best-ideas/string-searching/)</sup> |
| 标准库中的存在 | C++17 在 `<functional>` 中提供 `std::boyer_moore_searcher`，供 `std::search` 使用<sup>[6](https://www.cppreference.com/cpp/utility/functional/boyer_moore_searcher)</sup> |

## 算法的工作原理

朴素搜索把模式串依次对齐到文本的每一个位置，自左向右比较字符；对于长度为 n 的文本和长度为 m 的模式串，大约需要 n·m 次比较。博耶–摩尔算法有两处不同。第一，它自右向左比较模式串，因此往往只检查一个文本字符，就在模式串的最后一个字符处发现失配。第二，一旦发生失配，它按两条预先计算好的规则所允许的位移移动模式串，取两者中较大的位移<sup>[4](https://www.cs.tufts.edu/comp/150GEN/classpages/BoyerMoore.html)</sup><sup> • </sup><sup>[7](https://cnds.constructor.university/courses/mfcs-2025/mfcs-notes.pdf)</sup>。

**坏字符规则。** 若在自右向左的扫描中，文本字符 T(k) 与模式串字符 P(i) 失配，则可以把模式串右移，使 T(k) 在模式串中的下一次出现（若有）与该文本位置对齐；位移量为 max(1, i − R(T(k)))，其中 R 记录每个字符在模式串中最靠右的位置<sup>[4](https://www.cs.tufts.edu/comp/150GEN/classpages/BoyerMoore.html)</sup>。在较大的字母表上，失配的文本字符往往根本不在模式串中出现，于是整个模式串便直接滑过该位置。

**好后缀规则。** 若 t 是模式串中与当前对齐位置上的文本相匹配的最长后缀，则可以移动模式串，使 t 在模式串中上一次出现的位置与文本对齐，其思路与克努特–莫里斯–普拉特（Knuth–Morris–Pratt）算法所用的移位表相同<sup>[4](https://www.cs.tufts.edu/comp/150GEN/classpages/BoyerMoore.html)</sup><sup> • </sup><sup>[8](https://xlinux.nist.gov/dads/HTML/boyermoore.html)</sup>。算法把两条规则结合起来，同时计算两种位移并取最大值<sup>[9](https://www.cs.man.ac.uk/~david/courses/advalgorithms/strings.pdf)</sup>。

## 性能

该算法的一个不同寻常之处是：粗略地说，模式串越长，它跑得越快；而且它是亚线性的——它检查的字符数一般少于它越过的字符数<sup>[2](https://www.cs.utexas.edu/~moore/best-ideas/string-searching/)</sup>。按原论文自己的测量，对于长度为5的随机英文模式串，算法在位置 i 处找到匹配之前通常只检查文本的 i/4 个字符，其实现平均执行的机器指令少于 i + patlen 条<sup>[1](https://www.cin.ufpe.br/~paguso/courses/if767/bib/Boyer_1977.pdf)</sup>。

最坏情况则不那么理想。如科尔所证明，博耶–摩尔算法在最坏情况下约需 3n 次比较<sup>[5](https://ir.cwi.nl/pub/5367/5367D.pdf)</sup>。1979年，当时正在研究字符串匹配复杂度的计算机科学家[兹维·加利](https://www.edgepedia.org/zh/zvi-galil)利（Zvi Galil）给出了该算法的一种修改，使其最坏情况运行时间变为线性，即使文本中包含模式串的多次出现也是如此。后来的工作进一步推进了这一界限：1994年《算法杂志》（*Journal of Algorithms*）上的一篇论文提出了一种由博耶–摩尔衍生的算法，最坏情况下进行 2n 次字符比较，而博耶–摩尔需要 3n 次；对于字符数多于两个的字母表，其平均比较次数也更少，代价是预处理平均耗时 O(m)、最坏情况下为 O(m²)<sup>[10](https://www.sciencedirect.com/science/article/abs/pii/S019667748471008X)</sup>。

## 小字母表与变体

在 DNA 这样的小字母表上，这一经典算法失去了大部分优势。由于子串频繁重现，跳跃距离往往不再随模式串长度增长；记住更多已匹配的内容可以产生更大的跳跃，甚至可以对每个文本字符至多查看一次，但这需要非常大的表或状态机<sup>[2](https://www.cs.utexas.edu/~moore/best-ideas/string-searching/)</sup>。2007年，马蒂亚斯·苏斯蒂克（Matyas Sustik）与摩尔描述了一种变体，以 UTCS 技术报告 TR-07-62《小字母表上的字符串搜索》（"String Searching over Small Alphabets"）发表，它只需一张小表就能在小字母表上取得良好性能<sup>[2](https://www.cs.utexas.edu/~moore/best-ideas/string-searching/)</sup>。

若干简化版本与相关算法被广泛使用：

- **博耶–摩尔–霍斯普尔（Boyer–Moore–Horspool）**只保留一张坏字符式的表。
- **科门茨–沃尔特（Commentz–Walter）**把这一思路推广到同时搜索多个字符串，其作用类似于阿霍–科拉西克（Aho–Corasick）算法<sup>[8](https://xlinux.nist.gov/dads/HTML/boyermoore.html)</sup>。

## 实际应用

据摩尔说，博耶–摩尔是已知最快的“在一个字符串中查找另一个字符串”方法的基础，也是 emacs 等一些文本编辑器中进行搜索时所选用的算法<sup>[2](https://www.cs.utexas.edu/~moore/best-ideas/string-searching/)</sup><sup> • </sup><sup>[9](https://www.cs.man.ac.uk/~david/courses/advalgorithms/strings.pdf)</sup>。自 C++17 起，标准库提供 `std::boyer_moore_searcher`：这是一个由模式串的迭代器构造的搜索器对象，实现了该算法，供 `std::search` 的 Searcher 重载使用；该头文件还提供 `std::boyer_moore_horspool_searcher`<sup>[6](https://www.cppreference.com/cpp/utility/functional/boyer_moore_searcher)</sup>。20世纪70年代，该算法还与克努特–莫里斯–普拉特算法一起，早期被用于计算生物学，通过精确匹配来定位共同的 DNA 区域<sup>[11](https://pmc.ncbi.nlm.nih.gov/articles/PMC4296134/)</sup>；它也是 BLAST 等一些近似匹配工具的基础<sup>[12](https://www.proquest.com/scholarly-journals/exact-pattern-matching-adapting-boyer-moore/docview/1955127472/se-2)</sup>。

## References

1. [Robert S. Boyer and J Strother Moore (1977). A Fast String Searching Algorithm. Communications of the ACM 20(10).](https://www.cin.ufpe.br/~paguso/courses/if767/bib/Boyer_1977.pdf)
2. [J Strother Moore. The Boyer-Moore Fast String Searching Algorithm, UTCS.](https://www.cs.utexas.edu/~moore/best-ideas/string-searching/)
3. [A fast string searching algorithm, CiNii Research (bibliographic record, DOI 10.1145/359842.359859).](https://cir.nii.ac.jp/crid/1361699995558729216?lang=en)
4. [The Boyer-Moore Algorithm, Tufts CS 150 class notes.](https://www.cs.tufts.edu/comp/150GEN/classpages/BoyerMoore.html)
5. [Crochemore et al. (CWI). Notes stating Cole's 3n worst-case bound for Boyer–Moore.](https://ir.cwi.nl/pub/5367/5367D.pdf)
6. [std::boyer_moore_searcher, cppreference.com.](https://www.cppreference.com/cpp/utility/functional/boyer_moore_searcher)
7. [Boyer-Moore Rules Combined, MFCS 2025 course notes, Constructor University.](https://cnds.constructor.university/courses/mfcs-2025/mfcs-notes.pdf)
8. [Boyer-Moore, Dictionary of Algorithms and Data Structures, NIST.](https://xlinux.nist.gov/dads/HTML/boyermoore.html)
9. [String searching: The Boyer-Moore algorithm, University of Manchester course notes.](https://www.cs.man.ac.uk/~david/courses/advalgorithms/strings.pdf)
10. [Fastest Pattern Matching in Strings. Journal of Algorithms 16(2), March 1994, pp. 163–189.](https://www.sciencedirect.com/science/article/abs/pii/S019667748471008X)
11. [Alignment-free genetic sequence comparisons: a review of recent approaches by word analysis. PMC.](https://pmc.ncbi.nlm.nih.gov/articles/PMC4296134/)
12. [Exact pattern matching: Adapting the Boyer-Moore algorithm for DNA searches. ProQuest.](https://www.proquest.com/scholarly-journals/exact-pattern-matching-adapting-boyer-moore/docview/1955127472/se-2)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Numerical, string, and geometric algorithms › String algorithms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/boyer-moore-string-search-algorithm

English original: https://www.edgepedia.org/boyer-moore-string-search-algorithm
