KMP算法

#include <iostream>
using namespace std;

void originalMatch(char* o,char* m)
{
	int original = strlen(o);
	int match = strlen(m);
	if (match > original||match ==0||original == 0)
	{
		cout<<"length is incorrect!"<<endl;
		return;
	}
	bool haveornot = true;
	for (auto i =0;i<original-match+1;i++)
	{
		bool flag = true;
		for (auto j =0;j < match;j++)
		{
			if(o[i+j]!=m[j])
			{
					flag =false;
					break;
			}
		}
		if (flag)
		{
			haveornot =false;
			cout<<"find the match string,the place is "<<i+1<<endl;
		}
	}
	if (haveornot)
	{
		cout<<"there is no string \""<<m<<"\" in the string \""<<o<<"\"!"<<endl;
	}
}

void Kmp(char* o,char* m)
{
	int original = strlen(o);
	int match = strlen(m);
	if (match > original||match ==0||original == 0)
	{
		cout<<"length is incorrect!"<<endl;
		return;
	}
	int* a =new int[match];
	int q = 0;
	a[0] = 0;
	bool haveornot = true;
	for (auto i =1;i<match;i++)
	{
		while(q>0&&m[q]!=m[i])
			q =  a[q];
		if (m[q] == m[i])
			q++;
		a[i] = q;
	}
	q=0;
	for (auto i =0;i<original;i++)
	{
		while(q>0&&m[q]!=o[i])
			q =  a[q-1];//减一是为了保证前面已匹配的  向右移动的位数,q加1表示匹配位置,运行到这时q表示匹配不成功所以移动前面的
		if (m[q] == o[i])
			q++;
		if (q == match)
		{
			haveornot =false;
			cout<<"find the match string,the place is "<<(i-q+1)+1<<endl;
			q=a[q-1];//保证匹配成功后面所要移动的位置
			//此处不需要保证i的后移,因为i后移了就会使成立m[q]!=o[i]
		}
	}
	if (haveornot)
	{
	cout<<"there is no string \""<<m<<"\" in the string \""<<o<<"\"!"<<endl;
	}
	delete []a;
}
void main()
{
	char o[100] = "anwsanwwqeasdfaanwsanwadadwanwsanwsanw";
	char m[100] ="anwsanw";
	originalMatch(o,m);
	cout<<"kmp\n";
	Kmp(o,m);
	system("pause");
}

### KMP算法的实现 KMP算法是一种高效的字符串匹配算法,它通过构建部分匹配表(也称为`next`数组)来减少不必要的回溯操作[^2]。以下是基于Python语言的KMP算法实现: ```python def compute_next_array(pattern): next_arr = [-1] * len(pattern) i, j = 0, -1 while i < len(pattern) - 1: if j == -1 or pattern[i] == pattern[j]: i += 1 j += 1 next_arr[i] = j else: j = next_arr[j] return next_arr def kmp_search(text, pattern): m, n = len(text), len(pattern) next_arr = compute_next_array(pattern) i, j = 0, 0 while i < m and j < n: if j == -1 or text[i] == pattern[j]: i += 1 j += 1 else: j = next_arr[j] if j == n: return i - j # 返回匹配起始位置 return -1 # 表示未找到匹配项 ``` 上述代码分为两部分: - `compute_next_array()` 函数用于计算模式串的部分匹配表(即`next`数组)。这部分的核心在于利用已知的最大公共前后缀长度来优化后续匹配过程[^5]。 - `kmp_search()` 函数则负责执行具体的字符串匹配逻辑。 --- ### KMP算法的应用场景 #### 文本编辑器中的查找功能 在文本编辑器中,当用户输入一段文字并希望快速定位某个关键词时,可以采用KMP算法完成这一任务。相比传统的暴力匹配方法,KMP能够在更短的时间内返回结果,尤其适用于大规模文档环境下的搜索需求[^1]。 #### 数据清洗与预处理 在大数据领域,经常需要对海量日志文件或其他形式的数据集进行过滤或提取特定字段的操作。此时如果目标子串固定不变,则可预先生成对应的`next`数组,在多次查询过程中显著提升效率[^3]。 #### 生物信息学研究 DNA序列由四种碱基组成(A,T,C,G),因此对于某些基因片段的研究工作而言,频繁涉及相似结构单元之间的对比分析。借助于KMP技术,研究人员能够更加便捷地识别出感兴趣的区域及其分布规律[^4]。 --- ### 性能优势总结 总体来看,由于引入了额外的信息存储机制——即所谓的“失败指针”,使得整个流程无需反复跳转至初始状态重新尝试;从而大幅降低了最坏情况下的时间开销,并保持相对稳定的内存占用水平[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值