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
is the encrypted 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 Input
JSOI07
Sample Output
I0O7SJ
HINT
The length of the data string for \ (100\%\) does not exceed \ (100000\) .
Solution
The suffix array is a bare question. The answer is \ (S[sa[i] + n-1] (s=input+input,sa[i]<=n) \)
#include <cstring>#include <cstdio>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <cmath>using namespaceStd#define N 200001#define REP (I, A, b) for (int i = A; I <= b; i++)#define DRP (i, A, b) for (int i = A; I >= b; i--)#define LL Long Longinline intRead () {intx =0, flag =1;Charch = getchar (); while(!isdigit (CH)) {if(! (ch ^'-')) flag =-1; ch = getchar (); } while(IsDigit (ch)) x = (x <<1) + (x <<3) + CH-' 0 ', ch = getchar ();returnx * FLAG;}intNCharIn[n];intS[n];intSa[n], T1[n], t2[n], c[n], rk[n], height[n];voidGet_sa () {int*x = T1, *y = t2, M = Wu; Rep (I,1, n) c[x[i] = s[i]]++; Rep (I,1, m) c[i] + = c[i-1]; DRP (i, N,1) sa[c[x[i]]--] = i; for(intK =1; K <= N; K <<=1) {intp =0; Rep (i, N-k +1, n) y[++p] = i; Rep (I,1, N)if(Sa[i] > k) y[++p] = sa[i]-K; Memset (c,0,sizeof(c)); Rep (I,1, n) c[x[i]]++; Rep (I,1, m) c[i] + = c[i-1]; DRP (i, N,1) sa[c[x[y[i]]]--] = Y[i]; Swap (x, y), p =0, x[sa[1]] = ++p; Rep (I,2, N) x[sa[i]] = y[sa[i-1] [= Y[sa[i]] && y[sa[i-1] + K] = = Y[sa[i] + K]? P: ++p;if((M = p) = = N) Break; }}voidGet_height () {Rep (I,1, N) rk[sa[i]] = i;intK =0; Rep (I,1, N) {if(k) k--;intp = sa[rk[i]-1]; while(S[i + K] = = s[p + K]) k++; Height[rk[i]] = k; }}intMain () {scanf ("%s", In +1); n = strlen (in +1); Rep (I,1, n) in[i + n] = In[i], s[n + i] = s[i] = (In[i] >=' A '&& In[i] <=' Z ') ? In[i]-' A '+1: In[i]-' A '+1+ -; n + = n; Get_sa (); Rep (I,1, N)if(Sa[i] <= N/2) printf ("%c", In[sa[i] + N/2-1]);return 0;}
bzoj1031 [JSOI2007] character encryption cipher