-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI.cpp
97 lines (85 loc) · 1.76 KB
/
I.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <bits/stdc++.h>
using namespace std;
#define cerr cerr << "DEBUG "
constexpr int N = 1e5 + 10;
int n;
string s;
int rnk[20][N], lg;
int p[N];
bool cmp(int i, int j) {
if (rnk[lg - 1][i] != rnk[lg - 1][j]) {
return rnk[lg - 1][i] < rnk[lg - 1][j];
}
i += (1 << (lg - 1));
j += (1 << (lg - 1));
if (max(i, j) >= n) {
return i > j;
}
return rnk[lg - 1][i] < rnk[lg - 1][j];
}
void build_sa() {
iota(p, p + n, 0);
sort(p, p + n, [&](int i, int j) {
return s[i] < s[j];
});
lg = 0;
rnk[lg][p[0]] = 0;
for (int i = 1; i < n; ++i) {
rnk[lg][p[i]] = rnk[lg][p[i - 1]] + (s[p[i]] != s[p[i - 1]]);
}
for (lg = 1; lg < 20; ++lg) {
sort(p, p + n, cmp);
rnk[lg][p[0]] = 0;
for (int i = 1; i < n; ++i) {
rnk[lg][p[i]] = rnk[lg][p[i - 1]] + cmp(p[i - 1], p[i]);
}
}
--lg;
}
int lcp(int i, int j) {
int ret = 0;
for (int x = lg; x >= 0; --x) {
if ((1 << x) + max(i, j) > n) {
continue;
}
if (rnk[x][i] == rnk[x][j]) {
ret += 1 << x;
i += 1 << x;
j += 1 << x;
}
}
return ret;
}
int l[N], r[N], d[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tc;
cin >> tc;
while (tc--) {
cin >> s;
n = (int) s.size();
build_sa();
for (int i = 0; i < n - 1; ++i) {
d[i] = lcp(p[i], p[i + 1]);
int c = i - 1;
while (~c && d[c] > d[i]) {
c = l[c];
}
l[i] = c;
}
long long ans = 0;
for (int i = n - 2; i >= 0; --i) {
int c = i + 1;
while (c < n - 1 && d[c] >= d[i]) {
c = r[c];
}
r[i] = c;
//cerr << i << ' ' << d[i] << ' ' << l[i] << ' ' << c << '\n';
ans += 1ll * d[i] * (i - l[i]) * (c - i);
}
ans *= 2;
ans += 1ll * n * (n + 1) / 2;
cout << ans << '\n';
}
}