[Poj1509] glass beads [suffix automatic mechanism]

Source: Internet
Author: User

Question

Returns the minimum representation of a string.

Analysis

Of course, this question can be done in the smallest notation! But I want to learn the suffix!

We multiply the length of this string by two, then create Sam, and then run the smallest letter on Sam every time to find the length of N and then stop. If u is stopped, ANS = sT [u]. Len-n + 1

  

 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <map> 6  7 using namespace std; 8 const int maxn=20000+100; 9 char s[maxn];10 int n,T;11 struct state{12     map<char,int>next;13     int len,link;14 }st[4*maxn];15 int last,cnt,cur;16 void init(){17     last=cur=0;18     cnt=1;19     st[0].len=0;20     st[0].link=-1;21     st[0].next.clear();22 }23 void build(char c){24     cur=cnt++;25     st[cur].next.clear();26     st[cur].len=st[last].len+1;27     int p;28     for(p=last;p!=-1&&!st[p].next.count(c);p=st[p].link)29         st[p].next[c]=cur;30     if(p==-1)31         st[cur].link=0;32     else{33         int q=st[p].next[c];34         if(st[p].len+1==st[q].len)35             st[cur].link=q;36         else{37             int clone=cnt++;38             st[clone].next=st[q].next;39             st[clone].link=st[q].link;40             st[clone].len=st[p].len+1;41             for(;p!=-1&&st  [p].next[c]==q;p=st[p].link)42                 st[p].next[c]=clone;43             st[q].link=clone;st[cur].link=clone;44         }45     }46     last=cur;47 }48 int main(){49     scanf("%d",&T);50     for(int t=1;t<=T;t++){51         scanf("%s",s);52         n=strlen(s);53         for(int i=0;i<n;i++)54             s[i+n]=s[i];55         init();56         for(int i=0;i<n;i++)57             build(s[i]);58         for(int i=n;i<2*n;i++)59             build(s[i]);60         int u=0;61         for(int i=0;i<n;i++){62             for(int j=‘a‘;j<=‘z‘;j++){63                 if(st[u].next.count(j)){64                     u=st[u].next[j];65                     break;66                 }67             }68         }69         int ans=st[u].len-n+1;70         printf("%d\n",ans);71     }72 return 0;73 }
View code

 

[Poj1509] glass beads [suffix automatic mechanism]

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.