KMP算法(C++)

KMP算法与BF算法不一样的在于,当主串与子串不匹配时,主串不回溯,选择了子串回溯,大大提高了运算效率。

借用了next1【】数组,让子串回溯。get_next函数求next1【】数组,get_next函数的实现难点在于下列几行代码:

while (i < T.length)

{

if (j == 0 || T.chi == T.chj)

{

++i, ++j;

next1i = j;

}

else

j = next1j;

}

只要明确两点就容易理解:

1、T j == Tnextj,那么nextj+1的最大值为nextj+1。

2、T j != Tnextj,那么nextj+1可能的次最大值为next next\[j ]+1,以此类推即可求出nextj+1

cpp 复制代码
#include<iostream>
#include<string>
using namespace std;
int next1[1000];
typedef struct node
{
	char ch[251];
	int length=0;//串当前长度
}SString;
void get_next(SString T)
{
	int i = 1;//当前串正在匹配字符串位置,也是next数组的索引
	next1[1] = 0;
	int j = 0;
	while (i < T.length)
	{
		if (j == 0 || T.ch[i] == T.ch[j])
		{
			++i;
			++j;
			next1[i] = j;
		}
		else
			j = next1[j];
	}
}
int Index_KMP(SString S, SString T, int pos)//S主串,T子串,pos从主串pos位置开始匹配
{
	int i = pos, j = 1;//i为主串下标,j为子串下标
	while (i <= S.length && j <= T.length)
	{
		if (S.ch[i] == T.ch[j])//匹配,往下继续
		{
			i++;
			j++;
		}
		else
			j=next1[j];
	}
		if (j >= T.length) return i - T.length;//返回主串与子串匹配时,主串的第一个下标
		else return 0;
}
int main()
{
	SString  s;
	SString  t;
	cout << "输入主串长度:" ;
	cin >> s.length;
	cout << endl;
	cout << "输入子串长度:";
	cin >> t.length;
	cout << endl << "输入主串:";
	for (int i = 1; i <= s.length; i++)//从下标1开始储存
	{
		cin >> s.ch[i];
	}
	cout << endl << "输入子串:";
	for (int i = 1; i <= t.length; i++)
	{
		cin >> t.ch[i];
	}
	get_next(t);
	int a = Index_KMP(s, t, 1);
	cout <<endl<< a;
}
相关推荐
apocelipes7 小时前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
HjhIron9 小时前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩10 小时前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹12 小时前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术16 小时前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
浮生望17 小时前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法
黄敬峰17 小时前
面试必刷:从JS底层包装类到双指针,彻底搞懂字符串与回文算法
算法
地平线开发者1 天前
J6B vio scenario sample
算法
BothSavage2 天前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法