POJ 1509 Glass beads suffix automaton

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.