Description
Like to delve into the problem of JS classmate, and recently fascinated by the encryption method of thinking. One day, he suddenly came up with what he thought was the ultimate encryption: to make a circle of information that needs to be encrypted, it is clear that they have many different ways of reading. For example, it can be read as:
JSOI07 soi07j oi07js i07jso 07JSOI 7jsoi0 sort them by the size of the string: 07JSOI 7jsoi0 i07jso JSOI07 oi07js soi07j read the last column of characters: I0O7SJ, the word after the encryption String (in fact, this encryption method is very easy to crack, because it is suddenly thought out, then ^ ^). However, if the string you want to encrypt is too long, can you write a program to accomplish this task?
Input
The input file contains a row of strings to encrypt. Note that the contents of a string are not necessarily letters, numbers, or symbols.
Output
The output line is the encrypted string.
Sample InputJSOI07Sample OutputI0O7SJHINT
The length of the data string for 100% does not exceed 100000.
Suffix array First question
I heard this is the sa naked question ... But I didn't see it at first.
The string is copied twice times, and then SA finishes directly outputting the first character of the pre-N/2 character
#include <cstdio> #include <cstring> #include <iostream>using namespace std; #define N 200010char Ch[n] ; int a[n],v[n],n,k,p,q;int sa[2][n],rnk[2][n];inline void calc_sa (int sa[n],int rnk[n],int sa[n],int Rnk[N]) {for (int i=1;i<=n;i++) v[rnk[sa[i]]]=i; for (int i=n;i>=1;i--) if (sa[i]>k) sa[v[rnk[sa[i]-k]]--]=sa[i]-k; for (int i=n-k+1;i<=n;i++) sa[v[rnk[i]]]=i; for (int i=1;i<=n;i++) rnk[sa[i]]=rnk[sa[i-1]]+ (rnk[sa[i]]!=rnk[sa[i-1]]| | Rnk[sa[i-1]+k]!=rnk[sa[i]+k]);} inline void Get_sa () {p=0,q=1;a[0]=-1; memset (v,0,sizeof (v)); for (int i=1;i<=n;i++) v[a[i]]++; for (int i=1;i<=256;i++) v[i]+=v[i-1]; for (int i=1;i<=n;i++) sa[p][v[a[i]]--]=i; for (int i=1;i<=n;i++) rnk[p][sa[p][i]]=rnk[p][sa[p][i-1]]+ (a[sa[p][i]]!=a[sa[p][i-1]); For (K=1;k<n;k<<=1,swap (P,Q)) Calc_sa (Sa[p],rnk[p],sa[q],rnk[q]);} int main () {scanf ("%s", ch+1); N=strlen (ch+1); for (int i=1;i<=n;i++) {a[i]=cH[i]; A[i+n]=a[i]; Ch[i+n]=ch[i]; } n*=2; Get_sa (); Ch[0]=ch[n]; for (int i=1;i<=n;i++) if (SA[P][I]<=N/2) printf ("%c", ch[sa[p][i]-1]); return 0;}
bzoj1031 [JSOI2007] character encryption cipher