上一篇
在计算机科学中,BM算法(Boyer-Moore算法)是一种高效的C++字符串匹配算法,广泛应用于文本编辑器、搜索引擎和生物信息学等领域。与传统的暴力匹配不同,BM算法通过“坏字符规则”和“好后缀规则”跳过不必要的比较,从而显著提升高效字符串搜索的性能。
假设我们要在一个长度为 n 的文本中查找一个长度为 m 的模式串。暴力匹配的时间复杂度为 O(n×m),而BM算法在实际应用中平均时间复杂度接近 O(n/m),尤其当模式串较长时效率极高。
我们将分三步实现BM算法:
void buildBadCharTable(const std::string& pattern, std::vector<int>& badChar) { int m = pattern.length(); for (int i = 0; i < 256; ++i) { badChar[i] = -1; } for (int i = 0; i < m; ++i) { badChar[static_cast<unsigned char>(pattern[i])] = i; }}
void buildGoodSuffixTable(const std::string& pattern, std::vector<int>& goodSuffix) { int m = pattern.length(); std::vector<int> suffix(m); // 计算suffix数组 suffix[m - 1] = m; for (int i = m - 2; i >= 0; --i) { int j = i; while (j >= 0 && pattern[j] == pattern[m - 1 - (i - j)]) { --j; } suffix[i] = i - j; } // 初始化goodSuffix表 for (int i = 0; i < m; ++i) { goodSuffix[i] = m; } // 填充goodSuffix表 int j = 0; for (int i = m - 1; i >= 0; --i) { if (suffix[i] == i + 1) { for (; j < m - 1 - i; ++j) { if (goodSuffix[j] == m) { goodSuffix[j] = m - 1 - i; } } } } // 处理非前缀匹配的情况 for (int i = 0; i <= m - 2; ++i) { goodSuffix[m - 1 - suffix[i]] = m - 1 - i; }}
int boyerMooreSearch(const std::string& text, const std::string& pattern) { int n = text.length(); int m = pattern.length(); if (m == 0) return 0; std::vector<int> badChar(256, -1); std::vector<int> goodSuffix(m, 0); buildBadCharTable(pattern, badChar); buildGoodSuffixTable(pattern, goodSuffix); int s = 0; // 模式串相对于文本的偏移量 while (s <= n - m) { int j = m - 1; // 从右向左匹配 while (j >= 0 && pattern[j] == text[s + j]) { --j; } if (j < 0) { // 找到匹配 return s; } else { // 计算滑动距离 int badCharShift = j - badChar[static_cast<unsigned char>(text[s + j])]; int goodSuffixShift = goodSuffix[j]; s += std::max(badCharShift, goodSuffixShift); } } return -1; // 未找到}
#include <iostream>#include <vector>#include <algorithm>// 此处插入上面三个函数...int main() { std::string text = "ABAAABCDABCABC"; std::string pattern = "ABC"; int pos = boyerMooreSearch(text, pattern); if (pos != -1) { std::cout << "Pattern found at index: " << pos << std::endl; } else { std::cout << "Pattern not found." << std::endl; } return 0;}
通过本教程,我们详细讲解了Boyer-Moore算法实现的原理与C++代码。BM算法利用坏字符和好后缀规则实现跳跃式匹配,是高效字符串搜索的经典代表。掌握它不仅能提升编程能力,还能深入理解算法设计的巧妙之处。
希望这篇面向初学者的教程能帮助你轻松入门BM算法!记得多动手实践,尝试修改模式串和文本内容,观察匹配过程的变化。
本文由主机测评网于2025-12-22发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://vpshk.cn/20251211507.html