#include#include #include #define MAXSIZE 20 typedef struct String{ char ch[MAXSIZE]; int length; }String; void InitData(String str){ str.ch[MAXSIZE]; } //KMP 未优化 int Index(String str,String s){ int i=0,j=0; while(i nextval void get_nextval(String s,int nextval[]){ int i=0,j=-1; nextval[0]=-1; while(i < s.length-1) { if(j == -1 || s.ch[i] == s.ch[j]){ ++i,++j; if(s.ch[i]!=s.ch[j]) nextval[i]=j; else nextval[i]=nextval[j]; } else j=nextval[j]; } } //KMP 优化2 int Index_KMP2(String str,String s,int nextval[]){ int i=0,j=0; while(i



