Advertisement
Mlxa

### Суффиксный массив без векторов

Mar 1st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define long ll
  5. #define all(x) begin(x), end(x)
  6. #define array array_
  7.  
  8. const int N = 3e6;
  9.  
  10. int n;
  11. char s[N];
  12. int equiv[N];
  13. int temp[N];
  14. int array[N];
  15. int bucket_size[N];
  16.  
  17.  
  18. void build() {
  19.     for (int i = 0; i < n; ++i) {
  20.         equiv[i] = s[i] + 1;
  21.         ++bucket_size[equiv[i]];
  22.     }
  23.     int mb = 256;
  24.     for (int i = 1; i <= mb; ++i) {
  25.         bucket_size[i] += bucket_size[i - 1];
  26.     }
  27.     for (int i = 0; i < n; ++i) {
  28.         assert(array[bucket_size[equiv[i] - 1]] == 0);
  29.         array[bucket_size[equiv[i] - 1]++] = i;
  30.     }
  31.     fill_n(bucket_size, mb + 1, 0);
  32.    
  33.     for (int l = 1; l < n; l *= 2) {
  34.         for (int i = 0; i < n; ++i) {
  35.             ++bucket_size[equiv[i]];
  36.         }
  37.         mb = *max_element(equiv, equiv + n);
  38.        
  39.         for (int i = 1; i <= mb; ++i) {
  40.             bucket_size[i] += bucket_size[i - 1];
  41.         }  
  42.         for (int i = 0; i < n; ++i) {
  43.             int first = array[i] - l;
  44.             if (first < 0) {
  45.                 first += n;
  46.             }
  47.             temp[bucket_size[equiv[first] - 1]++] = first;
  48.         }
  49.         copy_n(temp, n, array);
  50.         fill_n(bucket_size, mb + 1, 0);
  51.        
  52.         temp[array[0]] = 1;
  53.         for (int a, b, c, d, i = 1; i < n; ++i) {
  54.             a = array[i];
  55.             b = array[i - 1];
  56.             c = a + l;
  57.             d = b + l;
  58.             if (c >= n) {
  59.                 c -= n;
  60.             }
  61.             if (d >= n) {
  62.                 d -= n;
  63.             }
  64.             temp[a] = temp[b];
  65.             if ((equiv[a] > equiv[b]) || (equiv[c] > equiv[d])) {
  66.                 ++temp[a];
  67.             }
  68.         }
  69.         copy_n(temp, n, equiv);
  70.     }
  71. }
  72.  
  73. int main() {
  74.     assert(freopen("input.txt", "r", stdin));
  75.     ios::sync_with_stdio(false);
  76.     cin.tie(nullptr);
  77.    
  78.     cin >> s;
  79.     n = (int)strlen(s);
  80.     s[n++] = '#';
  81.    
  82.     build();
  83.    
  84.     uint64_t h = 0;
  85.     for (int i = 1; i < n; ++i) {
  86.         h *= 3;
  87.         h += array[i];
  88.     }
  89.    
  90.     cout << h << '\n';
  91.     cout << 1000 * clock() / CLOCKS_PER_SEC << '\n';
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement