KMP
来源:互联网 发布:淘宝好友在哪里看 编辑:程序博客网 时间:2024/06/03 20:19
KMP是字符串的单匹配算法,判断一个字符串是不是另一个字符串的子串。
处理next数组
void find(){ int len=strlen(s2); int t1=0,t2=-1; net[0]=-1; while(t1<len) { if(t2==-1||s2[t1]==s2[t2]) net[++t1]=++t2; else t2=net[t2]; } return;}
判断过程
int kmp(){ int len1=strlen(s1); int len2=strlen(s2); int t1=0; int t2=0; while(t1<len1) { if(t2==-1||s1[t1]==s2[t2]) t1++,t2++; else t2=net[t2]; if(t2==len2) printf("%d\n",t1-t2+1),t2=net[t2]; }}
1 1