Link: poj 1961
Question:Returns the maximum number of times K (k> 1) of the substring that starts with the first character of the string and ends with the nth character. If yes, outputs I and K.
For example, aabaabaab has a length of 12.
To 2nd characters, a appears twice. to 6th characters, AAB appears twice,
Up to 9th characters, AAB appears 3 times, and up to 12th characters, AAB appears 4 times
Analysis: this is to find the maximum number of cycles for the substring prefix (including the entire string) of the given string. Just follow the next array.
If (N % (n-next [N]) = 0)
The minimum cycle length is L = N-next [N];
The maximum number of cycles is S = N/L = N/(n-next [N]).
# Include <stdio. h> # include <string. h> int next [1001010] = {-1}; char s [1001010]; void getnext (int n) {int I = 0, j =-1; while (I <n) {If (j =-1 | s [I] = s [J]) {I ++; j ++; next [I] = J;} else J = next [J];} int main () {int N, I, J, K = 0, M; while (scanf ("% d", & N )! = EOF) {If (n = 0) break; scanf ("% s", S); k ++; printf ("Test Case # % d \ n ", k); getnext (n); for (I = 1; I <n; I ++) {J = I + 1; if (J % (J-next [J]) = 0) {M = J/(J-next [J]); If (M> 1) printf ("% d \ n", J, m) ;}} printf ("\ n");} return 0 ;}