【数据结构】算法4.6-4.7 KMP算法
2016-10-10 20:16
295 查看
#include <stdio.h> #include <string.h> char S[20]="ABCDEFGHIJK"; char T[5]="DEFG"; int next[5]; int pos=0; /*******************************声明部分****************************************/ int index_KMP(char *s,char *t,int pos); //利用模式串的t的next函数求t在主串s中的第pos个位置之后的位置的KMP算法(t非空,1<=pos<=Strlength(s))。 void get_next(char * t,int * next); //求模式串t的next函数的并存入数组next[]中。 /*******************************函数部分****************************************/ int index_KMP(char *s,char *t,int pos) { int i=pos,j=1; while (i<=(int)strlen(s)&&j<=(int)strlen(t)){ if (j==0 || s[i]==t[j-1]){ //继续进行后续字符串的比较 i++; j++; } else j=next[j]; //模式串向右移动 } if (j>(int)strlen(t)) //匹配成功 return i-strlen(t)+1; else //匹配不成功 return 0; } void get_next(char *t,int *next) { int i=1,j=0; next[0]=next[1]=0; while (i<(int)strlen(t)) { if (j==0 || t[i]==t[j]){ i++; j++; next[i]=j; } //if else j=next[j]; } //while } /*******************************主函数部分**************************************/ int main() { int n; get_next(T,next); n=index_KMP(S,T,pos); if(n!=0) printf("\n模式串T在主串S中第%d个位置\n\n",n); else printf("\n主串中不存在与模式串相匹配的子串!\n\n"); return 0; }
相关文章推荐
- 数据结构第四次上机 第四章之BF和KMP算法(顺序串的各种模式匹配算法)
- 数据结构- 串的模式匹配算法:BF和 KMP算法
- 【算法与数据结构】字符串匹配之KMP算法
- 算法与数据结构-KMP算法
- 数据结构Java实现——③串--->串的模式匹配:Brute-Force算法和 KMP算法
- KMP算法(养成篇) – 数据结构和算法36
- 一步一步复习数据结构和算法基础-KMP算法
- 【数据结构&&算法系列】KMP算法介绍及实现(c++ && java)
- 【算法与数据结构】字符串匹配之KMP算法
- KMP算法(养成篇) - 数据结构和算法36
- KMP算法之NEXT数组代码原理分析 - 数据结构和算法38
- 【算法与数据结构】KMP算法
- KMP算法(养成篇) - 数据结构和算法36
- KMP算法之NEXT数组代码原理分析 - 数据结构和算法38
- 数据结构- 串的模式匹配算法:BF和 KMP算法
- KMP算法之最终实现及优化 - 数据结构和算法39
- 数据结构- 串的模式匹配算法:BF和 KMP算法
- 数据结构- 串的模式匹配算法:BF和 KMP算法
- 《数据结构》之串的模式匹配算法——KMP算法
- 数据结构- 串的模式匹配算法:BF和 KMP算法