To give a circular string, ask where to start is the dictionary order of the string is minimal.
Idea: The minimum representation and suffix automata, but I was to learn the suffix automata to write this problem, I did not learn the minimum notation.
The procedure is simple, first establish a suffix automaton, and then from the root start along the Tranc pointer from the A->Z, the point that arrives at the end of the string is the smallest dictionary order, the starting point as long as the length of +1 can be reduced.
Understanding of the suffix automata: http://wyfcyx.is-programmer.com/posts/76107.html
CODE:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 10010using namespace Std;struct Complex{complex *tranc[26],*father;short len; Mempool[max << 2],*c = Mempool,none,*nil = &none,*root,*last; Complex *newcomplex (int _) {C->len = _;fill (C->tranc,c->tranc + 26,nil); C->father = Nil;return C + +;} int T;char s[max];inline void Initialize () {C = Mempool;root = last = Newcomplex (0);} inline void Add (int c) {Complex *NP = Newcomplex (Last->len + 1), *p = Last;for (; p! = Nil && p->tranc[c] = = NI L p = p->father) p->tranc[c] = np;if (p = nil) Np->father = root;else {Complex *q = p->tranc[c];if (Q->len = = P >len + 1) np->father = q;else {Complex *nq = Newcomplex (P->len + 1); Nq->father = Q->father;q->father = NP ->father = nq;memcpy (nq->tranc,q->tranc,sizeof (Q->tranc)); for (; P! = Nil && p->tranc[c] = = q; p = P ->father) P->tranc[c] = NQ;}} last = NP;} int main (){for (CIN >> T; t--;) {Initialize (); scanf ("%s", s); int length = strlen (s); for (int i = 0; i < length; ++i) Add (S[i]-' a '); for (int i = 0; i < Length ++i) ADD (S[i]-' a '); Complex *now = root;for (int i = 0; i < length; ++i) for (int j = 0; J <; ++j) if (now->tranc[j]! = nil) {now = Now ->tranc[j];break;} printf ("%d\n", Now->len-length + 1);}}
POJ 1509 Glass beads suffix automaton