POJ 1509 Glass Beads suffix Automatic Machine
Give a circular string and ask where the Lexicographic Order of the string is the minimum.
Idea: the least representation method and the bare Question of the suffix automatic mechanism. However, I did not learn the least representation method when I wrote this question to learn the suffix automatic mechanism.
The procedure is very simple. First, create a suffix automatic machine, and then start from the root and follow the tranc pointer from a-> z to the point of len arrival, which is the end point of the string with the smallest Lexicographic Order, to calculate the starting point, you only need to subtract the length and then add 1.
Understanding of suffix-based automatic machines: www.bkjia.com
CODE:
#include
#include
#include
#include #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] == nil; 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 < 26; ++j)if(now->tranc[j] != nil) {now = now->tranc[j];break;}printf(%d,now->len - length + 1);}}