Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3374
It turns out that this is the smallest representation...
Method: Calculate the minimum expression and the maximum expression respectively to find the initial position, and then compare the brute force. Use KMP twice to find the maximum and minimum occurrences of the string.
# Define n 1000010 char STR [N], ss [N * 2], LIN [N]; int P [N]; int minrp (char * s, int L) {// minimum int I = 0, j = 1, K = 0, T; while (I <L & J <L & K <L) {T = s [(I + k)> = L? I + k-l: I + k]-s [(j + k)> = L? J + k-l: J + k]; If (! T) K ++; else {If (T> 0) I = I + k + 1; else J = J + k + 1; if (I = J) + + J; k = 0;} return I;} int maxrp (char * s, int L) {// maximum int I = 0, j = 1, k = 0, T; while (I <L & J <L & K <L) {T = s [(I + k)> = L? I + k-l: I + k]-s [(j + k)> = L? J + k-l: J + k]; If (! T) K ++; else {If (T <0) I = I + k + 1; else J = J + k + 1; if (I = J) + + J; k = 0;} return I;} void KMP () {int Len = strlen (Lin); int I, J, K; P [0] =-1; j =-1; for (I = 1; I <Len; I ++) {While (j> = 0 & lin [I]! = LiN [J + 1]) J = P [J]; If (LiN [I] = LiN [J + 1]) J ++; P [I] = J ;}} int KMP _ (char * s) {int Len = strlen (s); int n = strlen (Lin); int I, j =-1; int sum = 0; for (I = 0; I <Len; I ++) {While (j> = 0 & S [I]! = LiN [J + 1]) J = P [J]; If (s [I] = LiN [J + 1]) J ++; if (j = n-1 & I! = Len-1) {sum + +; j = P [J];} return sum;} int main () {While (scanf ("% s", STR )! = EOF) {int I, j, k; int Len = strlen (STR); int minr = minrp (STR, Len) + 1; int maxr = maxrp (STR, Len) + 1; strcpy (SS, STR); strcat (SS, STR); For (k = 0, I = minr-1; I <Len; I ++) {LiN [k ++] = STR [I];} for (I = 0; I <minr-1; I ++) {LiN [k ++] = STR [I];} LiN [k] = '\ 0'; KMP (); int mint = KMP _ (SS ); for (k = 0, I = maxr-1; I <Len; I ++) {LiN [k ++] = STR [I];} for (I = 0; I <maxr-1; I ++) {LiN [K + +] = STR [I];} LiN [k] = '\ 0'; KMP (); int maxt = KMP _ (SS); printf ("% d \ n", minr, mint, maxr, maxt);} return 0 ;}