0001 int* buildNext ( char* P ) { //构造模式串P的next表 0002 size_t m = strlen ( P ), j = 0; //“主”串指针 0003 int* next = new int[m]; int t = next[0] = -1; //next表,首项必为-1 0004 while ( j < m - 1 ) 0005 if ( 0 > t || P[t] == P[j] ) { //匹配 0006 ++t; ++j; next[j] = t; //则递增赋值:此处可改进... 0007 } else //否则 0008 t = next[t]; //继续尝试下一值得尝试的位置 0009 return next; 0010 }