// KMP.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
void GetNext(const char *strShort,int *next,int iLength)
{
int i=0;
int j=-1;
next[0]=-1;
while(i<iLength-1)
{
if(-1==j||*(strShort+j)==*(strShort+i))
{
i++;
j++;
next[i]=j;
}
else
{
j=next[j];
}
}
}
int KMPStringMatch(const char *strLong,const char *strShort)
{
int iLong=strlen(strLong);
int iShort=strlen(strShort);
if(iLong<iShort)
return -1;
int *next=(int*)malloc(sizeof(int)*iShort);
memset(next,0,sizeof(int)*iShort);
GetNext(strShort,next,iShort);
int i=0;
int j=0;
while (i<iShort&&j&l
字符串——KMP算法实现
最新推荐文章于 2024-04-14 17:48:07 发布