KMP匹配算法概念+实例

本文详细介绍了KMP算法中常见的两种next数组构造方法,包括经典模式串匹配失败后的跳转规则,并通过实例演示了如何计算next数组。两种next数组的区别在于处理匹配失败时的不同策略,有助于理解KMP算法的工作原理。

此文重点梳理两种最常见的next数组,以及在KMP匹配中的应用

符号说明

模式串:T[i]i=1,...,T_length主串:S[i]i=1,...,S_lengthnext数组:next[i]i=1,...,T_length 模式串:T[i] \quad i=1,...,T\_length\\ 主串:S[i] \quad i=1,...,S\_length\\ next数组:next[i]\quad i=1,...,T\_length\\ 模式串:T[i]i=1,...,T_length主串:S[i]i=1,...,S_lengthnext数组:next[i]i=1,...,T_length

第一种next数组

第一种next数组在KMP中的作用为:
当S[i]与T[j]在主串 S[i]S[i]S[i] 处匹配失败时,将指向模式串的指针 jjj 跳转到模式串next[j]next[j]next[j]
next[j]={0当j=1时Max{k∣1<k<j且“T1⋯Tk−1”=“Tj−k−1⋯Tj−1”}当此集合不为空时1其他情况 next[j]=\begin{cases} 0 &当j=1时\\ Max\{k|1<k<j且“T_1 \cdots T_{k-1}”=“T_{j-k-1}\cdots T_{j-1}”\} &当此集合不为空时\\ 1 &其他情况\\ \end{cases} next[j]=0Max{k∣1<k<jT1Tk1=Tjk1Tj1}1j=1当此集合不为空时其他情况
例1.T="aba"时next数组的计算过程

例2.T="abaabcac"时next数组的计算过程
由于aba在上例中已计算过,从j=4开始

通过两个例子,可以将next数组的公式总结为:

  1. j=1时,nextj]=0成立
  2. 当j ≠\neq= 1时,扫描1~j-1,得到最长前后缀长度;next[j]=最长前后缀长度+1
    (若无相等,则前后缀长度为0,next[j]=1。如例2中j=7时)
    注意:前缀和后缀可重叠

练习1.
求abababaab的next数组

011234562

代码实现

void get_next(int T_length) {//模式串next数组的生成
	int i = 1, j = 0;
	next[1] = 0;
	while (i <= T_length) {
		if (j == 0 || T[i] == T[j]) {
			i++;
			j++;
			next[i] = j;
		} else
			j = next[j];
	}
}

在KMP中的应用

int  Index_KMP(int S_length, int T_length, int pos) {//从pos开始的一次KMP匹配
	int i = pos, j = 1;
	while (i <= S_length && j <= T_length) {
		if (j == 0 || S[i] == T[j]) {
			i++;
			j++;
		} else
			j = next[j];
	}
	if (j > T_length) {
		printf("%d ", i - T_length);
		return i - T_length;
	} else return 0;//若匹配失败
}

第二种next数组

第一中next数组在KMP中的作用为:
当S[i]与T[j]匹配失败时,查询前一位 j-1 的next[j-1],跳过next[j-1]个数,从next[j-1]+1的位置开始重新匹配。
next[j]={0当j=1时Max{k∣1<k<j且“T1⋯Tk”=“Tj−k+1⋯Tj”}当此集合不为空时0其他情况 next[j]=\begin{cases} 0 &当j=1时\\ Max\{k|1<k<j且“T_1 \cdots T_{k}”=“T_{j-k+1}\cdots T_{j}”\} &当此集合不为空时\\ 0&其他情况\\ \end{cases} next[j]=0Max{k∣1<k<jT1Tk=Tjk+1Tj}0j=1当此集合不为空时其他情况
例3.T="aba"时next数组的计算过程

例4.T="abaabcac"时next数组的计算过程
由于aba在上例中已计算过,从j=4开始

通过两个例子,可以将next数组的公式总结为:
  1. j=1时,nextj]=0成立
  2. 当j ≠\neq= 1时,扫描1~j,得到最长前后缀长度;next[j]=最长前后缀长度
    (若无相等,则前后缀长度为0,next[j]=0。如例4中j=7时)

代码实现

void get_next(int T_length) {//模式串next数组的生成
	int i = 2, j = 1;
	next[1] = 0;
	while (i <= T_length) {
		if(j==1&&T[i]!=T[j]){
			next[i]=0;
			i++;
		}
		if ( T[i] == T[j]) {
			next[i] = j;
			i++;
			j++;
		} else {
			j = next[j-1] + 1;
		}
	}
}

在KMP中的应用

int  Index_KMP(int S_length, int T_length, int pos) {//从pos开始的一次KMP匹配
	int i = pos, j = 1;
	while (i <= S_length && j <= T_length) {
		if(j==1&&S[i]!=T[j]){
			i++;
		}
		if (S[i] == T[j]) {
			i++;
			j++;
		} else {
			j = next[j-1] + 1;
		}
	}
	if (j > T_length) {
		printf("%d ", i - T_length - 1);
		return i - T_length;
	} else return 0;//若匹配失败
}

完整实现:
一:

#include<bits\stdc++.h>
#define MaxSize 10000
char T[MaxSize], S[MaxSize];
int next[MaxSize];
void get_next(int T_length) {//模式串next数组的生成
	int i = 1, j = 0;
	next[1] = 0;
	while (i <= T_length) {
		if (j == 0 || T[i] == T[j]) {
			i++;
			j++;
			next[i] = j;
		} else
			j = next[j];
	}
}
int  Index_KMP(int S_length, int T_length, int pos) {//从pos开始的一次KMP匹配
	int i = pos, j = 1;
	while (i <= S_length && j <= T_length) {
		if (j == 0 || S[i] == T[j]) {
			i++;
			j++;
		} else
			j = next[j];
	}
	if (j > T_length) {
		printf("%d ", i - T_length);
		return i - T_length;
	} else return 0;//若匹配失败
}
int main() {
	int i;
	int S_length ;//主串
	int T_length ;//模式串
	
	scanf("%d", &T_length);
	scanf("%s", T + 1);
	scanf("%d", &S_length);
	scanf("%s", S + 1);

	get_next(T_length);

	for (int position = 1; position < S_length; position++ ) {
		position = Index_KMP(S_length, T_length, position);
		if (position == 0)
			break;
	}
	/*for (i = 1; i <= T_length-1; i++)//输出next数组
		printf("%d ", next[i]);
	printf("%d", next[i]);*/
	return 0;
}

二:

#include<bits\stdc++.h>
#define MaxSize 10000
char T[MaxSize], S[MaxSize];
int next[MaxSize];
void get_next(int T_length) {//模式串next数组的生成
	int i = 2, j = 1;
	next[1] = 0;
	while (i <= T_length) {
		if(j==1&&T[i]!=T[j]){
			next[i]=0;
			i++;
		}
		if ( T[i] == T[j]) {
			next[i] = j;
			i++;
			j++;
		} else {
			j = next[j-1] + 1;
		}
	}
}
int  Index_KMP(int S_length, int T_length, int pos) {//从pos开始的一次KMP匹配
	int i = pos, j = 1;
	while (i <= S_length && j <= T_length) {
		if(j==1&&S[i]!=T[j]){
			i++;
		}
		if (S[i] == T[j]) {
			i++;
			j++;
		} else {
			j = next[j-1] + 1;
		}
	}
	if (j > T_length) {
		printf("%d ", i - T_length - 1);
		return i - T_length;
	} else return 0;//若匹配失败
}
int main() {
	int i;
	int S_length ;//主串
	int T_length ;//模式串

	scanf("%d", &T_length);
	scanf("%s", T + 1);
	scanf("%d", &S_length);
	scanf("%s", S + 1);

	get_next(T_length);

	for (int position = 1; position < S_length; position++ ) {
		position = Index_KMP(S_length, T_length, position);
		if (position == 0)
			break;
	}
	/*printf("\n");
	for (i = 1; i <= T_length - 1; i++) //输出next数组
		printf("%d ", next[i]);
	printf("%d", next[i]);*/
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快苏排序OAO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值