char S[100] = "oooSusakeooo", P[100] = "Susake"; int s_next[100]; int KMP(int pos, int len1, int len2){ int i = pos, j = 1, k = 0; s_next[1] = 0; while (j < len1) if (k == 0 || P[k - 1] == P[j - 1]) j++, k++, s_next[j] = k; else k = s_next[k]; j = 1; while (i <= len2 && j <= len1){ if (j == 0 || S[i - 1] == P[j - 1]) i++, j++; else j = s_next[j]; } return j > len1 ? i - len1 : 0; } //KMP(0, strlen(P), strlen(S));
时间: 2024-09-15 20:42:53