-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathSuffixArray.cpp
153 lines (120 loc) · 3.38 KB
/
SuffixArray.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/************************************************************************
Suffix Array. Builing works in O(NlogN).
Also LCP array is calculated in O(NlogN).
This code counts number of different substrings in the string.
Based on problem I from here: http://codeforces.ru/gym/100133
************************************************************************/
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>
using namespace std;
const int MAXN = 205000;
const int ALPH = 256;
const int MAXLOG = 20;
int n;
char s[MAXN];
int p[MAXN]; // suffix array itself
int pcur[MAXN];
int c[MAXN][MAXLOG];
int num[MAXN];
int classesNum;
int lcp[MAXN];
void buildSuffixArray() {
n++;
for (int i = 0; i < n; i++)
num[s[i]]++;
for (int i = 1; i < ALPH; i++)
num[i] += num[i - 1];
for (int i = 0; i < n; i++) {
p[num[s[i]] - 1] = i;
num[s[i]]--;
}
c[p[0]][0] = 1;
classesNum = 1;
for (int i = 1; i < n; i++) {
if (s[p[i]] != s[p[i - 1]])
classesNum++;
c[p[i]][0] = classesNum;
}
for (int i = 1; ; i++) {
int half = (1 << (i - 1));
for (int j = 0; j < n; j++) {
pcur[j] = p[j] - half;
if (pcur[j] < 0)
pcur[j] += n;
}
for (int j = 1; j <= classesNum; j++)
num[j] = 0;
for (int j = 0; j < n; j++)
num[c[pcur[j]][i - 1]]++;
for (int j = 2; j <= classesNum; j++)
num[j] += num[j - 1];
for (int j = n - 1; j >= 0; j--) {
p[num[c[pcur[j]][i - 1]] - 1] = pcur[j];
num[c[pcur[j]][i - 1]]--;
}
c[p[0]][i] = 1;
classesNum = 1;
for (int j = 1; j < n; j++) {
int p1 = (p[j] + half) % n, p2 = (p[j - 1] + half) % n;
if (c[p[j]][i - 1] != c[p[j - 1]][i - 1] || c[p1][i - 1] != c[p2][i - 1])
classesNum++;
c[p[j]][i] = classesNum;
}
if ((1 << i) >= n)
break;
}
for (int i = 0; i < n; i++)
p[i] = p[i + 1];
n--;
}
int getLcp(int a, int b) {
int res = 0;
for (int i = MAXLOG - 1; i >= 0; i--) {
int curlen = (1 << i);
if (curlen > n)
continue;
if (c[a][i] == c[b][i]) {
res += curlen;
a += curlen;
b += curlen;
}
}
return res;
}
void calcLcpArray() {
for (int i = 0; i < n - 1; i++)
lcp[i] = getLcp(p[i], p[i + 1]);
}
int main() {
assert(freopen("substr.in","r",stdin));
assert(freopen("substr.out","w",stdout));
gets(s);
n = strlen(s);
buildSuffixArray();
// Now p from 0 to n - 1 contains suffix array of original string
/*for (int i = 0; i < n; i++) {
printf("%d ", p[i] + 1);
}*/
calcLcpArray();
long long ans = 0;
for (int i = 0; i < n; i++)
ans += n - p[i];
for (int i = 1; i < n; i++)
ans -= lcp[i - 1];
cout << ans << endl;
return 0;
}