-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSubstring Distribution.cpp
178 lines (167 loc) · 4.81 KB
/
Substring Distribution.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
#include<bits/stdc++.h>
using namespace std;
#define INF 2047483647
#define INFL 9223372036854775807
#define ll long long
#define pii pair<ll,ll>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define ull unsigned long long
#define M 1000000007
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define take(x) scanf("%d",&x)
#define DE(x) printf("\ndebug %d\n",x);
#define vout(x) for(int i=0;i<x.size();i++) printf("%d ",x[i]);
#define pie acos(-1)
#define MOD 998244353
// Finds the longest common prefix of two consecutive element of suffix array
class Substring{
public:
string s;
int n;
vector<int> suffix_array,lcp,c;
Substring(string str){
s = str;
s+="$";
n = s.size();
suffix_array_construction();
}
vector<int> sort_cyclic_shifts() {
// int n = s.size();
const int alphabet = 256;
vector<int> p(n),cnt(max(alphabet, n), 0);
c.resize(n);
for (int i = 0; i < n; i++)
cnt[s[i]]++;
for (int i = 1; i < alphabet; i++)
cnt[i] += cnt[i-1];
for (int i = 0; i < n; i++)
p[--cnt[s[i]]] = i;
c[p[0]] = 0;
int classes = 1;
for (int i = 1; i < n; i++) {
if (s[p[i]] != s[p[i-1]])
classes++;
c[p[i]] = classes - 1;
}
vector<int> pn(n), cn(n);
for (int h = 0; (1 << h) < n; ++h) {
for (int i = 0; i < n; i++) {
pn[i] = p[i] - (1 << h);
if (pn[i] < 0)
pn[i] += n;
}
fill(cnt.begin(), cnt.begin() + classes, 0);
for (int i = 0; i < n; i++)
cnt[c[pn[i]]]++;
for (int i = 1; i < classes; i++)
cnt[i] += cnt[i-1];
for (int i = n-1; i >= 0; i--)
p[--cnt[c[pn[i]]]] = pn[i];
cn[p[0]] = 0;
classes = 1;
for (int i = 1; i < n; i++) {
pair<int, int> cur = {c[p[i]], c[(p[i] + (1 << h)) % n]};
pair<int, int> prev = {c[p[i-1]], c[(p[i-1] + (1 << h)) % n]};
if (cur != prev)
++classes;
cn[p[i]] = classes - 1;
}
c.swap(cn);
}
return p;
}
void suffix_array_construction() {
suffix_array = sort_cyclic_shifts();
}
vector<int> get_suffix_array(){
vector<int> sorted_shifts = suffix_array;
sorted_shifts.erase(sorted_shifts.begin());
return sorted_shifts;
}
vector<int> get_lcp(){
vector<int> _lcp = lcp;
_lcp.erase(_lcp.begin());
return _lcp;
}
int getSubstringNumber(string t){
int m = t.size();
int L = 0,R = n-1;
bool isPres = true;
for(int i=0;i<m;i++){
int l = L,r = R;
int res = R+1;
// upper bound
while(l<=r){
int mid = (l+r)/2;
if( suffix_array[mid]+i>=n or s[ suffix_array[mid]+i ]<=t[i] ){
l = mid+1;
}else{
res = mid;
r = mid-1;
}
}
if( res == 0 or suffix_array[res-1]+i>=n or s[ suffix_array[res-1]+i ] != t[i] ){
isPres = false;
break;
} else{
R = res-1;
}
// lower bound
l = L; r = R;
res = -1;
while(l<=r){
int mid = (l+r)/2;
if( suffix_array[mid]+i>=n or s[ suffix_array[mid]+i ]<t[i] ){
l = mid+1;
}else{
res = mid;
r = mid-1;
}
}
if( res == -1 or suffix_array[res]+i>=n or s[ suffix_array[res]+i ] != t[i] ){
isPres = false;
break;
} else{
L = res;
}
}
if( isPres ){
return R-L+1;
} else{
return 0;
}
}
void createLCP(){
int k = 0;
lcp.resize(n);
for(int i=0;i<n-1;i++){
int pi = c[i];
int j = suffix_array[pi-1];
while( s[i+k] == s[j+k] ) k++;
lcp[pi] = k;
k = max(k-1,0);
}
}
};
int main(){
string s = "abab";
cin>>s;
int n = s.size();
int cum[n+2] = {0};
Substring sbstr = Substring(s);
vector<int> suffix = sbstr.get_suffix_array();
sbstr.createLCP();
vector<int> lcp = sbstr.get_lcp();
cum[1]++;
cum[n-suffix[0]+1]--;
for(int i=1;i<lcp.size();i++){
cum[lcp[i]+1]++;
cum[n-suffix[i]+1]--;
}
for(int i=1;i<=n;i++) cum[i]+=cum[i-1];
for(int i=1;i<=n;i++) cout<<cum[i]<<" ";
printf("\n");
}