#include <stdio.h> #include <string.h> #define MAXSTRLEN 100 typedef char SString[MAXSTRLEN+2]; void InputString(SString &str) { // 读取字符串 scanf("%s", str + 1); // 首先用scanf读取字符串 str[0] = strlen(str + 1); // 求出字符串的长度并保存在str[0]中 } void get_next(SString T, int *next) { // 算法4.7 int i = 1; next[1] = 0; int j = 0; while (i < T[0]) { if (j == 0 || T[i] == T[j]) { ++i; ++j; next[i] = j; } else j = next[j]; } } int main(){ SString str; int next[MAXSTRLEN+2]; int i; InputString(str); get_next(str, next); for(i=1; i<=str[0]; i++){ printf("%d ",next[i]); } return 0; } /************************************************************** Problem: 2151 User: admin Language: C++ Result: Accepted Time:8 ms Memory:1144 kb ****************************************************************/