Suffix array---woj 1564 problem 1564-a-Circle

Source: Internet
Author: User

problem 1564-a-Circle problem ' s Link: http://acm.whu.edu.cn/land/problem/detail?problem_id=1564

Mean:

Give you a string of numbers not exceeding 1e6, and find the first position in the front of the large ring number string.

Analyse:

OK, I admit this is a water problem, the game when the SB, because the original to do after the automatic machine to solve the string of the ring minimum notation, so always with the knowledge of the suffix automata to set K small notation, the game is too stubborn.

The problem is the use of the suffix array sa[] array, sa[i]=k means that all the suffixes of the string are sorted by dictionary order, and the suffix of I is ranked K.

Then we simply copy the number string to the back of the original string (ring), to the S to find the suffix number to get the SA array, and then found Sa<=n k SA, then sa[i]-1 is the answer.

Why? Look at this picture:

concatenated strings We can get 2*n strings, but we just have to care about the first n strings, because the next n strings actually don't work at all, they're just duplicates of the prefix of the first n strings.

So we just need to find the sa[i]<=n to meet the first K, sa[i]-1 is the answer.

Time Complexity:o (n)

Source Code:  

//Memory Time//1347K 0MS//by:crazyacking//2015-04-20-19.00#include <map>#include<queue>#include<stack>#include<cmath>#include<cstdio>#include<vector>#include<string>#include<cstdlib>#include<cstring>#include<climits>#include<iostream>#include<algorithm>#defineMAXN 1000010#defineLL Long Longusing namespacestd;Const intMAXN =2002000;intRANK[MAXN],WB[MAXN],WV[MAXN],WSS[MAXN];BOOLcmpint(RNintAintBintl) {    returnR[A]==R[B] && r[a+l]==r[b+l];}voidDaint(RNint*sa,intNintm) {    inti,j,p,*x=rank,*y=wb,*T;  for(i=0; i<m;i++) wss[i]=0;  for(i=0; i<n;i++) wss[x[i]=r[i]]++;  for(i=1; i<m;i++) wss[i]+=wss[i-1];  for(i=n-1; i>=0; i--) sa[--wss[x[i]]]=i;  for(j=1, p=1;p <n;j*=2, m=p) { for(p=0, i=n-j;i<n;i++) y[p++]=i;  for(i=0; i<n;i++)if(SA[I]&GT;=J) y[p++]=sa[i]-J;  for(i=0; i<n;i++) wv[i]=X[y[i]];  for(i=0; i<m;i++) wss[i]=0;  for(i=0; i<n;i++) wss[wv[i]]++;  for(i=1; i<m;i++) wss[i]+=wss[i-1];  for(i=n-1; i>=0; i--) sa[--wss[wv[i]]]=Y[i];  for(t=x,x=y,y=t,p=1, x[sa[0]]=0, i=1; i<n;i++) X[sa[i]]=CMP (y,sa[i-1],sa[i],j)? p1:p + +; }    return;}CharS[MAXN];intR[MAXN],SA[MAXN];intMain () {intn,k;  while(~SCANF ("%d%d",&n,&k)) {GetChar ();            Gets (s); intLen =strlen (s), I;  for(inti=len;i<len*2; ++i) s[i]=s[i-Len]; S[len*2]=' /'; Len=len*2;  for(intI=0; i<len;++i) s[i]+='a'-'0'; Len++;  for(i=0; i<len-1; i++) R[i]= s[i]-'a'+1; R[len-1] =0; Da (R,sa,len, -); intidx=0;  for(intI=1; i<len;++i) {if(sa[i]+1<=N) {idx++; if(idx==k) printf ("%d\n", sa[i]!=0?sa[i]:n); }        }    }    return 0;}
View Code

Suffix array---woj 1564 problem 1564-a-Circle

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.