int KMP(char *s,char *t,int *next) { int i = 0,j = 0,lens = strlen(s),lent = strlen(t); while (i < lens && j < lent){ if (j == -1 || s[i] == t[j]) {i++; j++;} else j = next[j]; } if (j == lent) return (i - lent + 1); else return 0; } void get_next(char *str,int *next) { int j = -1,i=0,len = strlen(str); next[0] = -1; while (i < len){ if (j == -1 || str[i] == str[j]){ i++; j++; if (str[i] != str[j]) next[i] = j; else next[i] = next[j]; } else j = next[j]; } } 其中 next[]为辅助数组,t为需要匹配的串,s为即将进行匹配的串,过多不解释,等俺理解后再说,嘿嘿,留着,做题时先用着!