nickel2halide

bclgold

Dec 14th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. #include <cassert>
  8.  
  9. using namespace std;
  10.  
  11. const int MAXN = 30000 + 5;
  12.  
  13. struct sort_fst {
  14.     const string& s;
  15.     sort_fst(const string& s) : s(s) {}
  16.     bool operator()(int i, int j) { return s[i] < s[j]; }
  17. };
  18.  
  19. int N;
  20. string S;
  21.  
  22. int r, rs[2*MAXN], rk[2][2*MAXN], pw, sa[2*MAXN], sa2[2*MAXN];
  23.  
  24. int main() {
  25.     ifstream fin("bclgold.in");
  26.     ofstream fout("bclgold.out");
  27.  
  28.     fin >> N;
  29.     for (int i = 0; i < N; ++i) {
  30.         string ch;
  31.         fin >> ch;
  32.         S += ch;
  33.     }
  34.  
  35.     string conc = S + '$' + string(S.rbegin(), S.rend());
  36.     int n = N + 1 + N;
  37.  
  38.     for (int i = 0; i < n; ++i) sa[i] = i;
  39.     sort(sa, sa+n, sort_fst(conc));
  40.     r = 0;
  41.     for (int i = 1; i < n; ++i) {
  42.         if (conc[sa[i]] != conc[sa[i - 1]])
  43.             rs[++r] = i;
  44.         rk[0][sa[i]] = r;
  45.     }
  46.  
  47.     for (int l = 1; l < n; pw ^= 1, l <<= 1) {
  48.         for (int i = n - l; i < n; ++i)
  49.             sa2[rs[rk[pw][i]]++] = i;
  50.         for (int i = 0; i < n; ++i)
  51.             if (sa[i] >= l)
  52.                 sa2[rs[rk[pw][sa[i] - l]]++] = sa[i] - l;
  53.  
  54.         r = 0;
  55.         rk[pw^1][sa[0] = sa2[0]] = 0;
  56.         rs[0] = 0;
  57.         for (int i = 1; i < n; ++i) {
  58.             if (rk[pw][sa2[i]] != rk[pw][sa2[i-1]] ||
  59.                 sa2[i]+l >= n || sa2[i-1]+l >= n ||
  60.                 rk[pw][sa2[i]+l] != rk[pw][sa2[i-1]+l])
  61.  
  62.                 rs[++r] = i;
  63.             rk[pw^1][sa[i] = sa2[i]] = r;
  64.         }
  65.     }
  66.  
  67.     int i = 0, j = N + 1;
  68.     while (i+j < 2*N+1) {
  69.         fout << (rk[pw][i] < rk[pw][j] ? conc[i++] : conc[j++]);
  70.         if ((i+j-(N+1)) % 80 == 0) fout << "\n";
  71.     }
  72.     if ((i+j-(N+1)) % 80 != 0) fout << "\n";
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment