0
点赞
收藏
分享

微信扫一扫

数据结构——KMP算法及其实现

心智的年轮 2022-03-17 阅读 61

1.算法综述

(1)为什么KMP算法好:最直观的就是时间复杂度。

BF和KMP比较为例:其中主串长度为n,子串长度为m,主串的指针为i,子串的指针为j

BF是最简单的匹配算法,使用的是穷举思路,最坏的情况就是主串前面(n-m)个位置都部分匹配到子串的最后一位,即各比较了m次,最后m位各比较1次,总共比较次数:

(n-m)*m+m=(n-m+1)*m

当m<<n时,时间复杂度为O(n*m)

KMP算法不会像BF那样一旦出现不匹配,主串就回到上次开始地方的下一个值,子串立马回到开头。也就是说主串不回溯。子串重新开始匹配时的位置也进行了重定义。

--->定义一个next[j]数组,长度与子串长度一致。j在子串中的位置也对应在next相应位置。

 耐心看公式,j=0对应的是数组中第1个位置,也就是next[0]=-1;计算next的值时,前面和后面指针同时会往中间移动,j=1时不移动,j=2时最多移动0次,j=3时最多移动0次,j=4时最多移动1次,j=5时最多移动1次,j=6时最多移动2次,以此类推……,所以计算next数组的值的时间复杂度为O(m)。然后是字符匹配的过程,循环过程的时间复杂度也是O(m),由于m<<n,所以时间复杂度为O(n)。

(2)KMP算法的实现步骤:

 (3)代码实现(采用c++)

#include<iostream>
using namespace std;

// get the next aarray
void Get_Next(string x, int next[], int length)
{
	next[0] = -1;
	int j = 1; int k = 0; int ki = 0; int m;
	if (length > 1) {
		next[1] = 0;
		j++;
	}
	while (j < length && length >2)  //the length of hte next array
	{
		m = j / 2;
		ki = j - m;
		while (ki < j) {
			if (x[k] == x[ki]) {
				next[j]++;
				k++;
				ki++;
			}
			else {
				if (k == 0) { ki++; }
				if (k > 0) { k = 0; }
				next[j] = 0;
			}
		}
		j++;
		k = 0;
	}
}
void kmp(string a,string b,int next[])
{
	int i = 0, j = 0;    //i :point a    j:point b
	
	while (i<a.length()&&j<b.length())
	{
		if (a[i] == b[j]) {
			i++;
			j++;
		}
		else {
			if (j == 0) i++;
			else j = next[j];
		}
	}
	cout << endl << "*************" << endl;
	if (j == b.length() && a[i-1] == b[j-1])
		cout << "find it!";
	else cout << "it's not here.";
	cout << endl << "*************" << endl;
}
int main()
{
	string a,b;  //a : main string    b : substring
	cout << "input the main string:" << endl;
	cin >> a;
	cout << "input the substring:" << endl;
	cin >> b;
	int *next=new int[b.length()];
	for (int i = 0; i < b.length(); i++)  //initial the array
	{
		next[i] = 0;
	}

	Get_Next(b, next, b.length());  
	cout << "input the next array:" << endl;
	for (int i = 0; i < b.length(); i++)  //initial the array
	{
		cout << next[i] << " ";
	}
	kmp(a, b, next);
	delete next;
}

运行结果:

备注:例子随便运行的,暂时没看出什么问题,代码纯自己写的,没有看过别人的代码是怎么写的,好像有十行代码写出来的。本人代码能力很一般,若有错误欢迎指正。

 (4)算法优化

KMP算法还可以进行优化,以后有空继续写。

举报

相关推荐

0 条评论