# [BZOJ1031][JSOI2007]字符加密Cipher

[BZOJ1031][JSOI2007]字符加密Cipher

：把需要加密的信息排成一圈，显然，它们有很多种不同的读法。例如下图，可以读作：

JSOI07 SOI07J OI07JS I07JSO 07JSOI 7JSOI0把它们按照字符串的大小排序：07JSOI 7JSOI0 I07JSO JSOI07
OI07JS SOI07J读出最后一列字符：I0O7SJ，就是加密后的字符串（其实这个加密手段实在很容易破解，鉴于这是

JSOI07

I0O7SJ

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;

int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}

#define maxn 400010

char S[maxn];
int n, x[maxn], y[maxn], sa[maxn], Ws[maxn];

bool cmp(int* a, int p1, int p2, int l) { return a[p1] == a[p2] && a[p1+l] == a[p2+l]; }
void ssort() {
int m = 0;
for(int i = 1; i <= n; i++) Ws[x[i] = S[i]]++, m = max(m, x[i]);
for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1];
for(int i = n; i; i--) sa[Ws[x[i]]--] = i;
for(int j = 1, pos; pos < n; j <<= 1, m = pos) {
pos = 0;
for(int i = n - j + 1; i <= n; i++) y[++pos] = i;
for(int i = 1; i <= n; i++) if(sa[i] > j) y[++pos] = sa[i] - j;
for(int i = 1; i <= m; i++) Ws[i] = 0;
for(int i = 1; i <= n; i++) Ws[x[i]]++;
for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1];
for(int i = n; i; i--) sa[Ws[x[y[i]]]--] = y[i];
swap(x, y); pos = 1; x[sa[1]] = 1;
for(int i = 2; i <= n; i++) x[sa[i]] = cmp(y, sa[i], sa[i-1], j) ? pos : ++pos;
}
return ;
}

int main() {
scanf("%s", S + 1);

n = strlen(S + 1);
for(int i = n + 1; i <= (n << 1); i++) S[i] = S[i-n];
n <<= 1;
ssort();
for(int i = 1; i <= n; i++) if(sa[i] <= (n >> 1)) putchar(S[sa[i]-1?sa[i]-1:n]); putchar('\n');

return 0;
}


posted @ 2017-03-01 19:01  xjr01  阅读(206)  评论(0编辑  收藏  举报