[BZOJ1031][JSOI2007]字符加密Cipher
试题描述
输入
输出
输出一行,为加密后的字符串。
输入示例
JSOI07
输出示例
I0O7SJ
数据规模及约定
对于100%的数据字符串的长度不超过100000。
题解
把字符串复制一遍后缀排序。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <algorithm> #include <vector> using namespace std; int read() { 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; }
[BZOJ1031][JSOI2007]字符加密Cipher
原文:http://www.cnblogs.com/xiao-ju-ruo-xjr/p/6485732.html