Last active
August 8, 2016 07:09
-
-
Save liuqinh2s/2cf1b6b3121cdf8ebd04b2edef3ff541 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int KMP(Str str, Str substr, int next[]){ | |
int i=0,j=0; | |
while(i<str.length && j<substr.length){ | |
if(j==-1||str.ch[i] == substr.ch[j]){ | |
++i; | |
++j; | |
} | |
else{ | |
j=next[j]; | |
} | |
} | |
if(j==substr.length){ | |
return i-substr.length; | |
} | |
else return -1; | |
} | |
void getnext(Str substr, int next[]){ | |
int i=0,j=-1; | |
next[0]=-1; | |
while(i<substr.length-1){ | |
if(substr.ch[i]==substr.ch[j]){ | |
++i; | |
++j; | |
next[i] = j; | |
} | |
else{ | |
j=next[j]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment