DescriptionLike 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 InputJSOI07Sample OutputI0O7SJHINT
The length of the data string for 100% does not exceed 100000.
SourceSolution
The so-called order, in fact, is to copy the string over the $sa$ value, the last character of each string corresponds to the $s[sa[i]+n-1]$
1#include <bits/stdc++.h>2 using namespacestd;3 Chars[200005];4 intsa[200005], wv[2][350005], tong[200005];5 6 BOOLcmpint*tmp,intXintYintj)7 {8 returnTMP[X] = = Tmp[y] && tmp[x + j] = = Tmp[y +j];9 }Ten One voidGetsa (intNintm) A { - intp =0, *x = wv[0], *y = wv[1]; - for(inti =0; I < n; ++i) the++tong[x[i] =S[i]]; - for(inti =1; I < m; ++i) -Tong[i] + = tong[i-1]; - for(inti = n-1; ~i; --i) +Sa[--tong[x[i]] =i; - for(intj =1; P! = N; J <<=1, M =p) + { Ap =0; at for(inti = n-j; I < n; ++i) -y[p++] =i; - for(inti =0; I < n; ++i) - if(Sa[i] >= j) y[p++] = Sa[i]-J; - for(inti =0; I < m; ++i) -Tong[i] =0; in for(inti =0; I < n; ++i) -++Tong[x[y[i]]; to for(inti =1; I < m; ++i) +Tong[i] + = tong[i-1]; - for(inti = n-1; ~i; --i) theSa[--tong[x[y[i] []] =Y[i]; *Swap (x, y), p =1, x[sa[0]] =0; $ for(inti =1; I < n; ++i)Panax NotoginsengX[sa[i]] = cmp (y, Sa[i-1], Sa[i], j)? P-1: p++; - } the } + A intMain () the { + intN; -CIN >>s; $n =strlen (s); $ for(inti =0; I < n; ++i) -S[i + N] =S[i]; -Getsa (n <<1|1, -); the for(inti =1; I <= n <<1; ++i) - if(Sa[i] < n) cout << S[sa[i] + N-1];Wuyicout <<Endl; the return 0; -}
View Code
[BZOJ1031] [JSOI2007] character encryption cipher (suffix array)