forked from ShahjalalShohag/code-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Suffix Array Isomorphic.cpp
218 lines (216 loc) · 6.04 KB
/
Suffix Array Isomorphic.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
const int kinds = N;
int str[N];
int buc[N], r[N], suf[N], X[N], Y[N], high[N];
bool cmp(int *r, int a, int b, int x) {
return (r[a] == r[b] && r[a + x] == r[b + x]);
}
void suffix_array_DA(int n, int m) {
int *x = X, *y = Y, i, j, k = 0, l;
for (i = 0; i <= max(n, m); i++) buc[i] = 0;
for (i = 0; i < n; i++) buc[x[i] = str[i]]++;
for (i = 1; i < m; i++) buc[i] += buc[i - 1];
for (i = n - 1; i >= 0; i--) suf[--buc[x[i]]] = i;
for (l = 1, j = 1; j < n; m = j, l <<= 1) {
j = 0;
for (i = n - l; i < n; i++) y[j++] = i;
for (i = 0; i < n; i++) if(suf[i] >= l) y[j++] = suf[i] - l;
for (i = 0; i < m; i++) buc[i] = 0;
for (i = 0; i < n; i++) buc[x[y[i]]]++;
for (i = 1; i < m; i++) buc[i] += buc[i - 1];
for (i = n - 1; i >= 0; i--) suf[--buc[x[y[i]]]] = y[i];
for (swap(x, y), x[suf[0]] = 0, i = 1, j = 1; i < n; i++) {
x[suf[i]] = cmp(y, suf[i - 1], suf[i], l) ? j - 1 : j++;
}
}
for (i = 1; i < n; i++) r[suf[i]] = i;
for (i = 0; i < n - 1; high[r[i++]] = k) {
for (k ? k-- : 0, j = suf[r[i] - 1]; str[i + k] == str[j + k]; k++);
}
}
struct SuffixArray {
int n;
vector<int> s;
vector<int> sa, rank, lcp;
static const int LG = 18;
vector<vector<int>> t;
vector<int> lg;
SuffixArray() {}
SuffixArray(vector<int> _s) {
n = _s.size();
s = _s;
for (int i = 0; i < n; i++) str[i] = s[i]; //for integers, each element should be positive
str[n] = 0;
suffix_array_DA(n + 1, kinds);
for (int i = 1; i <= n; i++) sa.push_back(suf[i]);
rank.resize(n);
for (int i = 0; i < n; i++) rank[sa[i]] = i;
costruct_lcp();
prec();
build();
}
void costruct_lcp() {
int k = 0;
lcp.resize(n - 1, 0);
for (int i = 0; i < n; i++) {
if (rank[i] == n - 1) {
k = 0;
continue;
}
int j = sa[rank[i] + 1];
while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++;
lcp[rank[i]] = k;
if (k) k--;
}
}
void prec() {
lg.resize(n, 0);
for (int i = 2; i < n; i++) lg[i] = lg[i / 2] + 1;
}
void build() {
int sz = n - 1;
t.resize(sz);
for (int i = 0; i < sz; i++) {
t[i].resize(LG);
t[i][0] = lcp[i];
}
for (int k = 1; k < LG; ++k) {
for (int i = 0; i + (1 << k) - 1 < sz; ++i) {
t[i][k] = min(t[i][k - 1], t[i + (1 << (k - 1))][k - 1]);
}
}
}
int query(int l, int r) { // minimum of lcp[l], ..., lcp[r]
int k = lg[r - l + 1];
return min(t[l][k], t[r - (1 << k) + 1][k]);
}
int get_lcp(int i, int j) { // lcp of suffix starting from i and j
if (i == j) return n - i;
int l = rank[i], r = rank[j];
if (l > r) swap(l, r);
return query(l, r - 1);
}
};
struct IsomorphicSuffixArray {
string s;
int n;
vector<int> sa, rank, lcp, a;
static const int LG = 18;
vector<vector<int>> t;
vector<int> lg;
SuffixArray S;
IsomorphicSuffixArray() {}
IsomorphicSuffixArray(string _s) {
s = _s;
n = s.size();
vector<int> last(26, -1);
a.resize(n);
for (int i = 0; i < n; i++) {
a[i] = last[s[i] - 'a'] == -1 ? 1 : i - last[s[i] - 'a'] + 1;
last[s[i] - 'a'] = i;
}
S = SuffixArray(a);
sa.resize(n);
iota(sa.begin(), sa.end(), 0);
sort(sa.begin(), sa.end(),
[&](const int &i, const int &j) { // O(alpha)
for (int l = 0; l < n; ) {
int x = i + l, y = j + l;
if (x >= n) return true;
if (y >= n) return false;
int vx = a[x] > l + 1 ? 1 : a[x];
int vy = a[y] > l + 1 ? 1 : a[y];
if (vx != vy) return vx < vy;
l += max(1, S.get_lcp(x, y));
}
return false;
});
rank.resize(n);
for (int i = 0; i < n; i++) rank[sa[i]] = i;
costruct_lcp();
prec();
build();
}
int get_lcp_brute(const int &i, const int &j) { // O(alpha)
for (int l = 0; l < n; ) {
int x = i + l, y = j + l;
if (x >= n || y >= n) return l;
int vx = a[x] > l + 1 ? 1 : a[x];
int vy = a[y] > l + 1 ? 1 : a[y];
if (vx != vy) return l;
l += max(1, S.get_lcp(x, y));
}
return n;
}
void costruct_lcp() {
lcp.resize(n - 1, 0);
for (int i = 0; i < n - 1; i++) {
lcp[i] = get_lcp_brute(sa[i], sa[i + 1]);
}
}
void prec() {
lg.resize(n, 0);
for (int i = 2; i < n; i++) lg[i] = lg[i / 2] + 1;
}
void build() {
int sz = n - 1;
t.resize(sz);
for (int i = 0; i < sz; i++) {
t[i].resize(LG);
t[i][0] = lcp[i];
}
for (int k = 1; k < LG; ++k) {
for (int i = 0; i + (1 << k) - 1 < sz; ++i) {
t[i][k] = min(t[i][k - 1], t[i + (1 << (k - 1))][k - 1]);
}
}
}
int query(int l, int r) { // minimum of lcp[l], ..., lcp[r]
int k = lg[r - l + 1];
return min(t[l][k], t[r - (1 << k) + 1][k]);
}
int get_lcp(int i, int j) { // lcp of suffix starting from i and j
if (i == j) return n - i;
int l = rank[i], r = rank[j];
if (l > r) swap(l, r);
return query(l, r - 1);
}
// occurrences of s[p, ..., p + len - 1]
pair<int, int> find_occurrence(int p, int len) {
p = rank[p];
pair<int, int> ans = {p, p};
int l = 0, r = p - 1;
while (l <= r) {
int mid = l + r >> 1;
if (query(mid, p - 1) >= len) ans.first = mid, r = mid - 1;
else l = mid + 1;
}
l = p + 1, r = n - 1;
while (l <= r) {
int mid = l + r >> 1;
if (query(p, mid - 1) >= len) ans.second = mid, l = mid + 1;
else r = mid - 1;
}
return ans;
}
};
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
string s;
cin >> s;
IsomorphicSuffixArray sa(s);
while (q--) {
int l, r;
cin >> l >> r;
--l, --r;
auto ans = sa.find_occurrence(l, r - l + 1);
cout << ans.second - ans.first + 1 << '\n';
}
return 0;
}
// https://www.hackerrank.com/challenges/similar-strings/problem