Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7a7f59

Browse files
Add LCP array build
1 parent 8a1eb19 commit e7a7f59

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

‎suffix_array.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ struct ranking {
88
ll second;
99
};
1010

11+
// P[i][j] holds the ranking of the j-th suffix
12+
// after comparing the first 2^i characters
13+
// of that suffix
14+
ll P[20][1000000];
15+
1116
bool comp(ranking a, ranking b) {
1217
if (a.first == b.first) {
1318
return a.second < b.second;
@@ -22,11 +27,6 @@ vector<ll> build_suffix_array(string s) {
2227
// vector to hold final suffix array result
2328
vector<ll> sa(n);
2429

25-
// P[i][j] holds the ranking of the j-th suffix
26-
// after comparing the first 2^i characters
27-
// of that suffix
28-
ll P[lgn][n];
29-
3030
// vector to store ranking tuples of suffixes
3131
vector<ranking> ranks(n);
3232

@@ -58,12 +58,34 @@ vector<ll> build_suffix_array(string s) {
5858
return sa;
5959
}
6060

61+
vector<ll> build_lcp(vector<ll> &sa, ll n) {
62+
63+
vector<ll> lcp(n,0);
64+
ll k, i, j , x, delta, step = ceil(log2(n));
65+
for (k = 1; k < n; k++) {
66+
i = k;
67+
j = k - 1;
68+
for (x = step; x >= 0; x--) {
69+
if (P[x][sa[i]] == P[x][sa[j]]) {
70+
delta = pow(2,x);
71+
lcp[i] += delta;
72+
i += delta;
73+
j += delta;
74+
}
75+
}
76+
}
77+
return lcp;
78+
}
79+
6180
int main() {
6281
string s;
82+
ll i, n, m, ans;
6383
cin >> s;
84+
n = s.length();
6485
vector<ll> sa = build_suffix_array(s);
65-
for(ll i = 0; i < s.length(); i++) {
66-
cout << i << " : " << sa[i] << endl;
86+
vector<ll> lcp = build_lcp(sa,n);
87+
for(ll i = 0; i < n; i++) {
88+
cout << i << " : " << sa[i] << " : " << s.substr(sa[i]) << " : " << lcp[i] << endl;
6789
}
6890
return 0;
6991
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /