KMP算法通过构建next数组优化字符串匹配,避免主串指针回溯。1. next数组记录模式串各位置最长相等前后缀长度,用于失配时跳转;2. 构建过程用双指针i和j,比较pattern[i]与pattern[j],相等则更新next[i]=j+1,不等则回退j=next[j-1];3. 匹配阶段遍历主串,字符相等时双指针进,不等且j>0时j回退,j=0则主串指针进;4. 当j等于模式串长时记录匹配位置并继续搜索。时间复杂度O(m+n)。完整实现包含buildNext与kmpSearch函数,示例中查找"ABABC"在"ABABDABACDABABCABC"中的位置,输出匹配起始下标。核心是利用模式串自身结构减少冗余比较。

在C++中实现KMP(Knuth-Morris-Pratt)字符串匹配算法,核心是通过预处理模式串生成一个部分匹配表(通常称为next数组),避免在匹配失败时回溯主串的指针,从而将时间复杂度优化到O(m + n)。
next数组记录的是模式串每个位置之前的最长相等前后缀长度。这个信息用于在匹配失败时决定模式串应该跳到哪个位置继续比较。
构建next数组的过程如下:
vector<int> buildNext(const string& pattern) {
int n = pattern.size();
vector<int> next(n, 0);
int j = 0;
for (int i = 1; i < n; ++i) {
while (j > 0 && pattern[i] != pattern[j]) {
j = next[j - 1];
}
if (pattern[i] == pattern[j]) {
j++;
}
next[i] = j;
}
return next;
}
使用构建好的next数组,在主串中查找模式串出现的位置。
立即学习“C++免费学习笔记(深入)”;
vector<int> kmpSearch(const string& text, const string& pattern) {
vector<int> matches;
if (pattern.empty()) return matches;
<pre class='brush:php;toolbar:false;'>vector<int> next = buildNext(pattern);
int m = text.size(), n = pattern.size();
int j = 0;
for (int i = 0; i < m; ++i) {
while (j > 0 && text[i] != pattern[j]) {
j = next[j - 1];
}
if (text[i] == pattern[j]) {
j++;
}
if (j == n) {
matches.push_back(i - n + 1);
j = next[j - 1]; // 继续找下一个匹配
}
}
return matches;}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
<p>int main() {
string text = "ABABDABACDABABCABC";
string pattern = "ABABC";</p><pre class='brush:php;toolbar:false;'>vector<int> result = kmpSearch(text, pattern);
cout << "Pattern found at positions: ";
for (int pos : result) {
cout << pos << " ";
}
cout << endl;
return 0;}
基本上就这些。KMP的关键在于理解next数组的含义——它保存了模式串自身的结构信息,使得我们可以在失配时跳过不必要的比较。只要把构建next和主匹配两个步骤写清楚,整个算法就很清晰了。
以上就是c++++怎么实现KMP字符串匹配算法_c++ KMP字符串匹配实现方法的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号