代码如下,自己看书去理解吧,哈哈! void getnext(char str[],int next[]) { next[0] = -1; int i=0,j=-1; while (i<strlen(str)) { if (j==-1||str[i]==str[j]) { i++; j++; next[i] = j; } else { j=next[j]; } } }