POJ 1509 Glass Beads suffix Automatic Machine

Source: Internet
Author: User

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);}}
   
  
 


 

Related Article

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.