BZOJ 1031 [JSOI2007]字符加密Cipher (后缀数组)

1031: [JSOI2007]字符加密Cipher

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 7593  Solved: 3291
[Submit][Status][Discuss]

Description

  喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考。一天,他突然想出了一种他认为是终极的加密办法
:把需要加密的信息排成一圈,显然,它们有很多种不同的读法。例如下图,可以读作:

 

JSOI07 SOI07J OI07JS I07JSO 07JSOI 7JSOI0把它们按照字符串的大小排序:07JSOI 7JSOI0 I07JSO JSOI07
 OI07JS SOI07J读出最后一列字符:I0O7SJ,就是加密后的字符串(其实这个加密手段实在很容易破解,鉴于这是
突然想出来的,那就^^)。但是,如果想加密的字符串实在太长,你能写一个程序完成这个任务吗?

Input

  输入文件包含一行,欲加密的字符串。注意字符串的内容不一定是字母、数字,也可以是符号等。

Output

  输出一行,为加密后的字符串。

Sample Input

JSOI07

Sample Output

I0O7SJ

HINT

对于100%的数据字符串的长度不超过100000。

Source

析:用后缀数组存储的两次字符串,然后只要的按字典序输出答案就好。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 10;
const int maxm = 3e5 + 10;
const ULL mod = 3;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

struct Array{
  int s[maxn], sa[maxn], t[maxn], t2[maxn];
  int h[maxn], r[maxn], c[maxn];
  int n;

  void init(){ n = 0;  memset(sa, 0, sizeof sa); }
  void build_sa(int m){
    int *x = t, *y = t2;
    for(int i = 0; i < m; ++i)  c[i] = 0;
    for(int i = 0; i < n; ++i)  ++c[x[i] = s[i]];
    for(int i = 1; i < m; ++i)  c[i] += c[i-1];
    for(int i = n-1; i >= 0; --i)  sa[--c[x[i]]] = i;

    for(int k = 1; k <= n; k <<= 1){
      int p = 0;
      for(int i = n-k; i < n; ++i)  y[p++] = i;
      for(int i = 0; i < n; ++i)  if(sa[i] >= k)  y[p++] = sa[i] - k;
      for(int i = 0; i < m; ++i)  c[i] = 0;
      for(int i = 0; i < n; ++i)  ++c[x[y[i]]];
      for(int i = 1; i < m; ++i)  c[i] += c[i-1];
      for(int i = n-1; i >= 0; --i)  sa[--c[x[y[i]]]] = y[i];

      swap(x, y);
      p = 1;  x[sa[0]] = 0;
      for(int i = 1; i < n; ++i)
        x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p-1 : p++;
      if(p >= n)  break;
      m = p;
    }
  }

  void getHight(){
    int k = 0;
    for(int i = 0; i < n; ++i)  r[sa[i]] = i;
    for(int i = 0; i < n; ++i){
      if(k)  --k;
      int j = sa[r[i]-1];
      while(s[i+k] == s[j+k])  ++k;
      h[r[i]] = k;
    }
  }

};
char s[maxn];
Array arr;

int main(){
  scanf("%s", s);
  arr.init();
  for(int i = 0; s[i]; ++i, ++n)  arr.s[arr.n++] = s[i];
  for(int i = 0; s[i]; ++i)  arr.s[arr.n++] = s[i];
  arr.s[arr.n++] = 0;
  arr.build_sa(130);
  arr.getHight();
  for(int i = 1; i <= n*2; ++i)
    if(arr.sa[i] < n)  printf("%c", arr.s[arr.sa[i]+n-1]);
  printf("
");
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/7819216.html