Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define all(x) begin(x),end(x)
- #define make_unique(x) if (!is_sorted(all(x))) sort(all(x)); x.erase(unique(all(x)), x.end());
- #define remax(x,y) (x > y ? x = y, true : false)
- #define remin(x,y) (x < y ? x = y, true : false)
- #define size(x) int(x.size())
- using namespace std;
- using ll = long long;
- struct SuffixArray {
- const string s;
- const int n;
- vector<int> arr;
- vector<int> lcp;
- #define modulo(x) (x >= n ? x - n : x)
- SuffixArray(const string& str)
- : s(str + char(0))
- , n(size(s))
- , arr(n)
- , lcp(n - 1)
- {
- vector<int> c(n), buff(n), ind(n), cnt(n);
- iota(all(arr), 0);
- vector<int> mp(256);
- for (int i = 0; i < n; i++) { mp[s[i]]++; }
- for (int i = 1; i < 256; i++) { mp[i] += mp[i - 1]; }
- for (int i = n - 1; i >= 0; i--) { arr[--mp[s[i]]] = i; }
- c[arr[0]] = 0;
- for (int i = 1; i < n; i++) {
- c[arr[i]] = c[arr[i - 1]];
- if (s[arr[i]] != s[arr[i - 1]]) {
- c[arr[i]]++;
- }
- }
- for (int deg = 1; (1 << (deg - 1)) < n; deg++) {
- const int len = 1 << (deg - 1);
- fill(all(cnt), 0);
- ind[0] = 0;
- for (int i = 0; i < n; i++) { arr[i] = modulo(arr[i] - len + n); }
- for (int i = 0; i < n; i++) { cnt[c[arr[i]]]++; }
- for (int i = 1; i < n; i++) { ind[i] = ind[i - 1] + cnt[i - 1]; }
- for (int i = 0; i < n; i++) { buff[ind[c[arr[i]]]++] = arr[i]; }
- copy(all(buff), begin(arr));
- buff[arr[0]] = 0;
- for (int i = 1; i < n; i++) {
- buff[arr[i]] = buff[arr[i - 1]];
- if (c[arr[i]] != c[arr[i - 1]] || c[modulo(arr[i] + len)] != c[modulo(arr[i - 1] + len)]) {
- buff[arr[i]]++;
- }
- }
- copy(all(buff), begin(c));
- }
- vector<int> pos(n);
- for (int i = 0; i < n; i++) {
- pos[arr[i]] = i;
- }
- int pre = 0;
- for (int i = 0; i < n - 1; i++) {
- int p = pos[i];
- int len = min(n - 1 - i, n - 1 - arr[p - 1]);
- int k = max(0, pre - 1);
- lcp[p - 1] = k;
- while (k < len && s[i + k] == s[arr[p - 1] + k]) { lcp[p - 1] = ++k; }
- pre = k;
- }
- }
- #undef modulo
- };
- struct DSU {
- vector<int> p, d;
- DSU (int n) : p(n), d(n) {
- fill(all(d), 1);
- iota(all(p), 0);
- }
- int get(int v) {
- if (p[v] == v) return v;
- return p[v] = get(p[v]);
- }
- void uni(int a, int b) {
- a = get(a), b = get(b);
- if (a == b) return;
- if (d[a] < d[b]) swap(a, b);
- p[b] = a;
- d[a] += d[b];
- }
- };
- ll Solve(const vector<int>& arr) {
- const int n = size(arr);
- DSU dsu(n);
- ll cur = 0;
- ll ans = 0;
- vector<vector<int>> mp(n + 1);
- for (int i = 0; i < n; i++) {
- mp[arr[i]].push_back(i);
- }
- vector<bool> used(n, false);
- for (int i = n; i >= 0; i--) {
- for (int v : mp[i]) {
- if (v > 0 && used[v - 1]) {
- int sz = dsu.d[dsu.get(v - 1)];
- cur -= 1ll * sz * (sz + 1) / 2;
- dsu.uni(v - 1, v);
- }
- if (v < n - 1 && used[v + 1]) {
- int sz = dsu.d[dsu.get(v + 1)];
- cur -= 1ll * sz * (sz + 1) / 2;
- dsu.uni(v, v + 1);
- }
- int sz = dsu.d[dsu.get(v)];
- cur += 1ll * sz * (sz + 1) / 2;
- used[v] = true;
- }
- ans += cur;
- }
- return ans;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- string s;
- cin >> s;
- SuffixArray suff(s);
- const auto& arr = suff.lcp;
- cout << Solve(arr) << '\n';
- }
Add Comment
Please, Sign In to add comment