串的模式匹配(基于KMP的匹配算法)

这是一个使用C语言实现的KMP(Knuth-Morris-Pratt)算法,用于在主串中查找模式串的匹配位置。程序首先生成随机的主串和模式串,然后计算模式串的next数组,最后通过KmpIndex函数应用KMP算法进行匹配,并输出匹配结果。
摘要由CSDN通过智能技术生成
#include<stdio.h>
#include<malloc.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#define ElemType char

typedef struct
{
	ElemType *start;//顺序字符串的起始位置
	int length;//字符串的长度,即串中的字符个数
}CommonStr;

int next[20]={0};//用于存放模式串的next值的数组

int CreateStr(CommonStr &pstr, int n)//生成用于测试的主串
{
	pstr.start=(ElemType *)malloc(100*sizeof(ElemType));
	int i,k=0;
	pstr.length=0;
	srand((unsigned)time(NULL));
	for(i = 0; i < n; i++)
	{
		pstr.start[i]=(char)(rand()%2 + 'a');
		pstr.length++;
	}
	pstr.start[i]='\0';
	return 1;
}

int GetNext(CommonStr str)
{//求模式串的next函数值并存入数组next中
	int j=0,k=-1;next[0]=-1;
	while(j 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Storm-Shadow

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

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

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

打赏作者

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

抵扣说明:

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

余额充值