Skip to content

Commit b6653ce

Browse files
committed
suffix array lexicograph loop
1 parent 3819334 commit b6653ce

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

Suffix-Array/sa.cpp

+19-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ struct SA {
3232
void buildLCP() {
3333
lcp = vector<int>(n);
3434
for(int i=0,k=0;i<n;i++) {
35-
if(ra[i] != n-1) {
36-
for(int j=sa[ra[i]+1]; s[i+k] == s[j+k];) k++;
35+
if(ra[i]) {
36+
for(int j=sa[ra[i]-1]; s[i+k] == s[j+k];) k++;
3737
lcp[ra[i]] = k;
3838
k = max(k-1, 0);
3939
}
@@ -51,3 +51,20 @@ struct SA {
5151
}
5252
bool findString(const string& t) {return findString(t, 0, t.size()-1);}
5353
};
54+
55+
int main() {
56+
string s;
57+
cin >> s;
58+
SA sa(s);
59+
int n = s.size();
60+
// lexicographical order looping, O(N^2)
61+
for(int i=1;i<=n;i++) {
62+
for(int j=sa.lcp[i]+1;j+sa.sa[i]<=n;j++) {
63+
for(int k=i;k<=n;k++) {
64+
if(k>i && sa.lcp[k]<j) break;
65+
int st = sa.sa[k], en = sa.sa[k]+j-1;
66+
cout << s.substr(st, j) << endl;
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)