#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
串的模式匹配(基于KMP的匹配算法)
最新推荐文章于 2021-04-04 23:35:57 发布
这是一个使用C语言实现的KMP(Knuth-Morris-Pratt)算法,用于在主串中查找模式串的匹配位置。程序首先生成随机的主串和模式串,然后计算模式串的next数组,最后通过KmpIndex函数应用KMP算法进行匹配,并输出匹配结果。
摘要由CSDN通过智能技术生成