kmp算法:如何最快地找到子串

1.遍历法在主串中寻找子串:遍历数组并且以每一个字母为基准对比字符串

int find(string major,string min)
{
	for (int x = 0; x < major.size(); x++)
	{	
		int copy = x;
		bool flag = true;
		for (int y = 0; y < min.size(); y++)
		{
			if (major[copy] == min[y])
			{
				copy++;
			}
			else
			{
				flag = false;
				break;
			}
		}
		if (flag == true)
		{
			return x;
		}
		
	}
	return -1;
}

遍历函数解析:在每次循环时,为了正常比对,设立了子指针copy与比对标志flag,最后将返回值+1的原因是字符串存在第0个字母但不存在第0个位置

以下为测试代码

#include <stdio.h>  
#include <stdlib.h>  
#include<iostream>
using namespace std;
int find(string major,string min)
{
	for (int x = 0; x < major.size(); x++)
	{	
		int copy = x;
		bool flag = true;
		for (int y = 0; y < min.size(); y++)
		{
			if (major[copy] == min[y])
			{
				copy++;
			}
			else
			{
				flag = false;
				break;
			}
		}
		if (flag == true)
		{
			return x;
		}
		
	}
	return -1;
}
int main()
{
	string major = "abcabd";
	string min = "abd";
	int position = find(major, min)+1;
	printf("%d", position);
	return 0;
}

可以看到,以上代码的缺陷在于在主串中寻找子串时,bcabd中寻早abd显然没有必要,因为第一个字母就是相同的,所以是否存在一种算法可以将这一步跳过呢?

首先先来认识一个概念:字符串最长相等前后缀,概念比较抽象,所以我们用例子来理解

abc的最长相等前后缀为0,aba的最长相等前后缀为a,abcab的最长相等前后缀为ab,abcba的最长相等前后缀为0

所以kpm算法的本质:在比较发现错误时,保持主串指针不动,在已匹配部分进行操作,将子串的最长相等前缀与主串的最长相等后缀对齐,子串对比指针从子串头向后移动到最长相等前后缀末尾,再与主串指针进行比对。

首先,先设置next数组用来记录子串不同位置的最长相等前后缀的长度,这个长度也是在比较发现错误时,子串比对指针向后移动的位置

next数组创建代码

typedef struct
{
	char data[100];
	int length;
}String;
void getnext(String child,int next[])
{
	int front = -1;
	int back = 0;
	next[0] = -1;
	while (back < child.length-1)
	{
		if (front == -1 || child.data[front] == child.data[back])
		{
			front++;
			back++;
			next[back] = front;
		}
		else
		{
			front = next[front];
		}
	}
}

代码解释:定义前后指针front和back,由于第一个前面没有字符串,所以next[0]赋值为-1,当后指针小于字符串长度减一时,因为next数组的最大后指针为子串长度-1,所以最后一次循环要保证back=strlen(next)-2可以顺利通过。如果前后指针字符匹配或者子串第一次进入循环时,前后指针分别加一,并且将后指针对应的next的值赋值为前指针的值,这里和一般循环不同的是这里的每一个next位置都要有值,所以无需接着循环就地储存,而如果不匹配,则将前指针回溯至前指针所在位置的最长相等前缀的末尾进行再一次比对

kmp算法实现代码

int kmp(String father, String child)
{
	int next[100];
	int fpoint = 0;
	int cpoint = 0;
	while (fpoint < father.length && cpoint < child.length)
	{
		if (cpoint == -1 || father.data[fpoint] == child.data[cpoint])
		{
			fpoint++;
			cpoint++;
		}
		else
		{
			cpoint = next[cpoint];
		}
	}
	if (cpoint >= child.length)
	{
		return fpoint - child.length+1;
	}
	else
	{
		return  0;
	}
}

定义主字符串和子字符串两个指针,如果相同则一起往后,如果不相同则按以上思路跳跃指针

以下为测试代码

#include <stdio.h>  
#include <stdlib.h>  
#include<string.h>
typedef struct
{
	char data[100];
	int length;
}String;
void getnext(String child,int next[])
{
	int front = -1;
	int back = 0;
	next[0] = -1;
	while (back < child.length-1)
	{
		if (front == -1 || child.data[front] == child.data[back])
		{
			front++;
			back++;
			next[back] = front;
		}
		else
		{
			front = next[front];
		}
	}
}
int kmp(String father, String child)
{
	int next[100];
	int fpoint = 0;
	int cpoint = 0;
	getnext(child, next);
	while (fpoint < father.length && cpoint < child.length)
	{
		if (cpoint == -1 || father.data[fpoint] == child.data[cpoint])
		{
			fpoint++;
			cpoint++;
		}
		else
		{
			cpoint = next[cpoint];
		}
	}
	if (cpoint >= child.length)
	{
		return fpoint - child.length+1;
	}
	else
	{
		return  0;
	}
}
int main()
{
	String father;
	strcpy_s(father.data, "abcabd");
	father.length = 6;
	String child;
	strcpy_s(child.data, "abd");
	child.length = 3;
int position=kmp(father, child);
printf("%d", position);

}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值