Returns a string with a length of n, the length of the loop prefix of the string, and the number of cycles;
Example: abababab
The first 4 characters, the cyclic string is AB, and there are 2 cyclic AB | AB
The first 6 characters. The cyclic string is AB and there are 3 cyclic AB | AB
The first 8 characters. The cyclic string is AB, and there are 4 cyclic AB | AB
Output:
4 2
6 3
8 4
[Cpp] # include <stdio. h>
# Define SIZE 1000006
Int next [SIZE];
Char str [SIZE];
Void getnext (int n)
{
Int I = 0, j =-1;
Next [0] =-1;
While (I <n ){
If (j =-1 | str [I] = str [j]) {
I ++;
J ++;
Next [I] = j;
}
Else {
J = next [j];
}
}
}
Int main ()
{
Int nT = 1, n, I, mixed, circulate;
While (scanf ("% d", & n), n ){
Getchar ();
Gets (str );
Getnext (n );
Printf ("Test case # % d \ n", nT ++ );
For (I = 1; I <= n; I ++ ){
Mixed = 2 * next [I]-I; // overlapping part
Circulate = next [I]-mixed; // Cycle Length
If (mixed> = 0 & I % circulate = 0 ){
Printf ("% d \ n", I, I/circulate );
}
}
Printf ("\ n ");
}
Return 0;
}