This time p [i]=P [j+1], when the array does not match, the j=next [j] statement is executed. Because of the mismatch, j will go back until j goes back to 0.
The code of kmp is as follows
one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen
//Substring and long string subscripts start from 1 boolkmp(char*s,intl1,char*p,intl2){ /*Find next array*/ for(inti=two,j=zero;i<=l1;i++){ while(j&&p[i]!=p[j+one]) j=next[j]; if(p[i]==p[j+one]) j++; next[i]=j; } /*Make string pairing*/ for(inti=zero;i<l1;i++){ while(j<l2&&s[i]==p[j+one]) j++; if(j==l2){ cout<<"Matching succeeded"<<endl; returntrue; } j=next[j]; } }