Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- #define rep(i, a, b) for(int i = a; i < (b); ++i)
- #define all(x) begin(x), end(x)
- #define sz(x) (int)(x).size()
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- struct SuffixArray {
- vi sa, lcp;
- SuffixArray(string& s, int lim=256) { // or basic_string<int>
- int n = sz(s) + 1, k = 0, a, b;
- vi x(all(s)+1), y(n), ws(max(n, lim)), rank(n);
- sa = lcp = y, iota(all(sa), 0);
- for (int j = 0, p = 0; p < n; j = max(1, j * 2), lim = p) {
- p = j, iota(all(y), n - j);
- rep(i,0,n) if (sa[i] >= j) y[p++] = sa[i] - j;
- fill(all(ws), 0);
- rep(i,0,n) ws[x[i]]++;
- rep(i,1,lim) ws[i] += ws[i - 1];
- for (int i = n; i--;) sa[--ws[x[y[i]]]] = y[i];
- swap(x, y), p = 1, x[sa[0]] = 0;
- rep(i,1,n) a = sa[i - 1], b = sa[i], x[b] =
- (y[a] == y[b] && y[a + j] == y[b + j]) ? p - 1 : p++;
- }
- rep(i,1,n) rank[sa[i]] = i;
- for (int i = 0, j; i < n - 1; lcp[rank[i++]] = k)
- for (k && k--, j = sa[rank[i] - 1];
- s[i + k] == s[j + k]; k++);
- }
- };
- int main() {
- string s; cin >> s;
- SuffixArray arr(s);
- ll n = s.size(), sum = n * (n + 1) / 2;
- sum -= accumulate(arr.lcp.begin(), arr.lcp.end(), 0LL);
- cout << sum << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement