Character Encryption cipherDescription
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 OutputI0O7SJ
start with a string array to sort it out, decisively super memory. Analyze the strings given:07jsoi-->07+......+i
7jsoi0-->7+......+0
I07jso-->i07+......+o
jsoi07-->jsoi07+ (7)
Oi07js-->oi07+......+s
Soi07j-->soi07+jyou can see that 07,7,i07,jsoi07,oi07,soi07 is all the suffixes of the original string, so we only need to sort these suffixes when sorting. This will use the suffix array. Note: Because it is annular, it is necessary to first copy the multiplication string, get the new string, and giveNew StringThe first half of the sequence, and then output each in the multiplication after the end position in the string.
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 200010;char Str[n];int *rank, Sa[n];int wa[n], wb[n], Wm[n];bool comp (int *r, int A, int b, int l) {return r[a] = = R[b] && R[a+l] = = R[b+l];} void Get_sa (char *r, int *sa, int n, int m) {int *x = WA, *y = WB, *t, I, J, p; for (i = 0; i < m; ++i) wm[i] = 0; for (i = 0; i < n; ++i) wm[x[i] = r[i]]++; for (i = 1; i < m; ++i) wm[i] + = wm[i-1]; for (i = n-1; I >= 0; i.) sa[--wm[x[i]] [i]; for (i = 0, j = 1, p = 0; p < n; j <<= 1, m = p) {for (P = 0, i = n-j; i < n; ++i) y[p++] = i; for (i = 0; i < n; ++i) if (Sa[i] >= j) y[p++] = sa[i]-J; for (i = 0; i < m; ++i) wm[i] = 0; for (i = 0; i < n; ++i) wm[x[y[i]]]++; for (i = 1; i < m; ++i) wm[i] + = wm[i-1]; for (i = n-1; I >= 0; i) sa[--wm[x[y[i]]] [= Y[i]; for (t = x, x = y, y = t, i = p = 1, x[sa[0]] = 0; i < n; ++i) { X[sa[i]] = Comp (y, sa[i], sa[i-1], j)? P-1: p++; }} rank = x;} int main () {while (gets (str) = NULL) {int len = strlen (str); for (int i = 0; i < len; i++) Str[i + len] = Str[i]; Str[2 * Len] = 0; Get_sa (str, SA, Len * 2 + 1, 256); for (int i = 0; I <= 2 * len; i++) if (Sa[i] < len) printf ("%c", Str[sa[i] + len-1]); printf ("\ n"); } return 0;}
Large field of view 1031-character encryption cipher (suffix array)