Topic: Given a string, the string of the last column after the string is sorted to the beginning of each character.
Legend of the suffix array 0.0 last night DC3 did not understand, so wrote a multiplier 0.0 Mr. Luo's 25 lines of code is really abstract Qaq Konjac Konjac Express Understanding can not qaq so I wrote a relatively clear version Qaq
First, this is the ring, so we add the first n-1 character of the string to the end of the string and then the suffix array.
After this, enumerate each beginning in the order of the suffix array to see if the length is greater than or equal to N.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 200200using namespace Std;int N,rank[m],sa[m];char s[m];int x[m],y[m];void get_rank () {int i,j,tot=0;static pair< Char,int>t[m];for (i=1;i<=n;i++) T[i]=make_pair (s[i],i); sort (t+1,t+n+1); for (i=1;i<=n;i++) {if (i==1| | T[i].first!=t[i-1].first) ++tot;rank[t[i].second]=tot;}} void Radix_sort (int key[],int order[]) {int i;static int sum[m],cnt[m],temp[m];memset (sum,0,sizeof sum); memset (cnt,0, sizeof CNT); for (i=1;i<=n;i++) sum[Key[i]]++;for (i=1;i<=n;i++) sum[i]+=sum[i-1];for (i=1;i<=n;i++) temp[sum [Key[order[i]]-1]+ ++cnt[Key[order[i]]]=order[i];memcpy (order,temp,sizeof temp);} void prefix_doubling () {int i,j,tot; Get_rank (); for (j=1;j<=n;j<<=1) {for (i=1;i<=n;i++) {x[i]=rank[i]; Y[i]=i+j>n?0:rank[i+j];sa[i]=i;} Radix_sort (Y,SA); Radix_sort (X,SA); for (i=1,tot=0;i<=n;i++) {if (I==1 | | X[sa[i]]!=x[sa[i-1] | | Y[SA[I]]!=Y[SA[I-1]]) ++tot;rank[sa[i]]=tot;}}} int main () {intI;SCANF ("%s", s+1); N=strlen (s+1); memcpy (s+n+1,s+1,n-1); n=n+n-1; Prefix_doubling (); for (i=1;i<=n;i++) if (sa[i]+ (n>>1) <=n) Putchar (s[sa[i]+ (n>>1)]);cout<< Endl;return 0;}
Bzoj 1031 JSOI2007 character encryption cipher suffix array