字符串的模式匹配—数据结构算法

功能截图:

部分源码,不含main主函数,如需源码可在小程序下载:

#include<stdio.h>
#include<string.h>

#define M 255
#define OK 1
#define ERROR 0

typedef char Str[M+1];

int StrAssign(Str s,char *ch){
int i;
if(strlen(ch)>M){
return ERROR;
}
else{
s[0]=strlen(ch);
for(i=1;i<=s[0];i++){
s[i]=*(ch+i-1);
}
return OK;
}
}
// adccfghhsadff
//str aaaasdfaa
//index 123456789
//next 000041112
int Next(Str T,int next[]){
int i=1,j=0;
next[i]=0;
while(i<T[0]){
if(j==0||T[i]==T[j]){
i++;//2,3,4,5
j++;//1,2,3,4
if(T[j]!=T[i]){
next[i]=j;
}
else{
next[i]=next[j];//
}
}
else
{
j=next[j];//j=0,
}
}
}

int Kmp(Str S,Str T,int next[]){
int i=1,j=1;
while(i<=S[0]&&j<=T[0]){
if(j==0||S[i]==T[j]){
i++;
j++;
}
else{
j=next[j];
}
}
if(j>T[0]){
return i-T[0];
}
else{
return 0;
}
}

  

注意这里仅为部分代码。获取源码请关注“值南针”微信公众号:可用电脑微信关注或手机关注(要最新版微信pc端)。在电脑方便。直接下载源码。

 

 

 

点击想要的算法

点击下载直接就可以用的。

posted @ 2020-01-19 19:21  值南针  阅读(283)  评论(0编辑  收藏  举报