"Portal: BZOJ1031" Brief test Instructions:
Given a string of length n, you can rotate the string ring, you can get n strings, such as: The original string is: JSOI07, you can get jsoi07,soi07j,oi07js,i07jso,07jsoi,7jsoi0, These strings are sorted from small to large in the dictionary order, outputting a new string consisting of the first letter of the nth string.
The following:
Violence obviously explodes, so use the suffix array to do, because the string ring can be rotated, so we put the original string on the original string, such as JSOI07, we then become jsoi07jsoi0, why the last 7 is missing, because if there is 7, will be repeated using JSOI07 this string , and then get an array of suffixes that will output the first letter.
Reference Code:
#include <cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<cstdlib>using namespacestd;Chara[210000];intsa1[210000];intrank[210000];intrsort[210000];intsa2[210000];inttt[210000];ints[210000];voidGet_sa (intNintm) {memcpy (rank,s,sizeof(Rank)); memset (Rsort,0,sizeof(Rsort)); for(intI=1; i<=n;i++) rsort[rank[i]]++; for(intI=1; i<=m;i++) rsort[i]+=rsort[i-1]; for(inti=n;i>=1; i--) sa1[rsort[rank[i]]--]=i; intln=1, p=0; while(p<N) {intk=0; for(inti=n-ln+1; i<=n;i++) sa2[++k]=i; for(intI=1; i<=n;i++)if(sa1[i]-ln>0) sa2[++k]=sa1[i]-Ln; memset (Rsort,0,sizeof(Rsort)); for(intI=1; i<=n;i++) rsort[rank[i]]++; for(intI=1; i<=m;i++) rsort[i]+=rsort[i-1]; for(inti=n;i>=1; i--) sa1[rsort[rank[sa2[i]]]--]=Sa2[i]; for(intI=1; i<=n;i++) tt[i]=Rank[i]; rank[sa1[1]]=1;p =1; for(intI=2; i<=n;i++) { if(tt[sa1[i]]!=tt[sa1[i-1]]|| tt[sa1[i]+ln]!=tt[sa1[i-1]+LN]) p++; Rank[sa1[i]]=p; } LN*=2; m=p; }}intMain () {scanf ("%s", A +1); intN=strlen (A +1); for(inti=n+1; i<=n+n-1; i++) a[i]=a[i-N]; for(intI=1; i<=n+n-1; i++) s[i]=A[i]; Get_sa (n+n-1, $); for(intI=1; i<=n+n-1; i++) { if(sa1[i]<=N) {printf ("%c", a[sa1[i]+n-1]); }} printf ("\ n"); return 0;}
BZOJ1031: [JSOI2007] character encryption cipher