cpp-library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: str/suffix_array.hpp

Depends on

Required by

Verified with

Code

#pragma once
#include "prelude.hpp"

// p[i] = starting position of i-th smallest shift
template <class It>
vector<int> sort_cyclic_shifts(It s, It s_last, int lim = 256) {
  int n = s_last - s;
  // c[i] = ordered tag of i-shift
  vector<int> p(n), p2(n), c(n), c2(n), cnt(max(n, lim), 0);

  rep(i, n) cnt[s[i]]++;
  rep(i, lim - 1) cnt[i + 1] += cnt[i];
  repr(i, n) p[--cnt[s[i]]] = i;
  int k = 0;
  rep(i, n - 1) c[p[i + 1]] = k += s[p[i]] != s[p[i + 1]];
  k++;

  for (int w = 1; w < n; w <<= 1) {
    rep(i, n) {
      int t = p[i] - w;
      p2[i] = t < 0 ? t + n : t;
    }
    fill(cnt.begin(), cnt.begin() + k, 0);
    rep(i, n) cnt[c[i]]++;
    rep(i, k - 1) cnt[i + 1] += cnt[i];
    repr(i, n) p[--cnt[c[p2[i]]]] = p2[i];
    c[p[0]] = k = 0;
    rep(i, n-1) {
      int piw = p[i] + w >= n ? p[i] + w - n : p[i] + w;
      int pi2w = p[i+1] + w >= n ? p[i+1] + w - n : p[i+1] + w;
      pair<int, int> prv(c[p[i]], c[piw]);
      pair<int, int> nxt(c[p[i + 1]], c[pi2w]);
      c2[p[i+1]] = k += prv != nxt;
    }
    k++;
    swap(c, c2);
  }
  return p;
}
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "str/suffix_array.hpp"

// p[i] = starting position of i-th smallest shift
template <class It>
vector<int> sort_cyclic_shifts(It s, It s_last, int lim = 256) {
  int n = s_last - s;
  // c[i] = ordered tag of i-shift
  vector<int> p(n), p2(n), c(n), c2(n), cnt(max(n, lim), 0);

  rep(i, n) cnt[s[i]]++;
  rep(i, lim - 1) cnt[i + 1] += cnt[i];
  repr(i, n) p[--cnt[s[i]]] = i;
  int k = 0;
  rep(i, n - 1) c[p[i + 1]] = k += s[p[i]] != s[p[i + 1]];
  k++;

  for (int w = 1; w < n; w <<= 1) {
    rep(i, n) {
      int t = p[i] - w;
      p2[i] = t < 0 ? t + n : t;
    }
    fill(cnt.begin(), cnt.begin() + k, 0);
    rep(i, n) cnt[c[i]]++;
    rep(i, k - 1) cnt[i + 1] += cnt[i];
    repr(i, n) p[--cnt[c[p2[i]]]] = p2[i];
    c[p[0]] = k = 0;
    rep(i, n-1) {
      int piw = p[i] + w >= n ? p[i] + w - n : p[i] + w;
      int pi2w = p[i+1] + w >= n ? p[i+1] + w - n : p[i+1] + w;
      pair<int, int> prv(c[p[i]], c[piw]);
      pair<int, int> nxt(c[p[i + 1]], c[pi2w]);
      c2[p[i+1]] = k += prv != nxt;
    }
    k++;
    swap(c, c2);
  }
  return p;
}
Back to top page