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 4780016

Browse files
Added suffix_array in untested_codes
1 parent f479caa commit 4780016

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

‎src/untested-codes/suffix_array.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <vector>
4+
#include <map>
5+
#include <set>
6+
#include <climits>
7+
#include <cstdio>
8+
#include <cstring>
9+
#include <cctype>
10+
#include <cassert>
11+
#include <cmath>
12+
#include <queue>
13+
using namespace std;
14+
15+
#define trace(x) {cerr << #x << "=" << x <<endl;}
16+
#define trace2(x, y) {cerr << #x << "=" << x << " " << #y << "=" << y <<endl;}
17+
template <typename T> ostream& operator<<(ostream& os, const vector<T> &p){os << "[ "; for (T x: p) os << x << " "; os << "]" << endl; return os;}
18+
template <typename T> ostream& operator<<(ostream& os, const set<T> &p){os << "{ "; for (T x: p) os << x << " "; os << "}" << endl; return os;}
19+
template <typename Tk, typename Tv> ostream& operator<<(ostream& os, const map<Tk, Tv> &p){os << "{ "; for (pair<Tk, Tv> x: p) os << x << " "; os << "}" << endl; return os;}
20+
template <typename Tk, typename Tv> ostream& operator<<(ostream& os, const pair<Tk, Tv> &p){os << "{" << p.first << ',' << p.second << "}";return os;}
21+
22+
typedef long long ll;
23+
24+
const int MOD = 1000000000+7;
25+
const int INF = 1000000000+5;
26+
const int MAX = 200005;
27+
28+
int lgn=0;
29+
int sa[25][1000005];
30+
int rankSuf[1000005];
31+
void constructSA(const char s[], int n) {
32+
cout << s << endl;
33+
map<int,int> rank;
34+
for (int i=0; i<n; i++) rank[s[i]]=0;
35+
int ctr=1;
36+
for (auto it=rank.begin(); it!=rank.end(); it++) it->second=ctr++;
37+
int p2=1;
38+
while (p2<=n) {p2<<=1; lgn++;}
39+
for (int i=0; i<n; i++) sa[0][i]=rank[s[i]];
40+
for (int i=1, l=1; i<=lgn; i++, l<<=1) {
41+
vector<pair<pair<int,int>,int>> rank;
42+
for (int j=0; j<n; j++) {
43+
pair<pair<int,int>, int> r = {{sa[i-1][j], ((j+l<n)?sa[i-1][j+l]:0)}, j};
44+
rank.push_back(r);
45+
}
46+
sort(rank.begin(), rank.end());
47+
for (int j=0, ctr=1; j<n; j++) {
48+
if (j>0 && (rank[j].first!=rank[j-1].first)) ctr++;
49+
sa[i][rank[j].second]=ctr;
50+
}
51+
}
52+
for (int i=0; i<n; i++) rankSuf[sa[lgn][i]-1]=i;
53+
}
54+
55+
int getLCP(int p, int q) {
56+
int l=0;
57+
for (int i=lgn, len=(1<<lgn); i>=0; i--, len>>=1) {
58+
if (sa[i][p]==sa[i][q]) {
59+
l+=len; p+=len, q+=len;
60+
}
61+
}
62+
return l;
63+
}
64+
65+
int lcp[25][1000005];
66+
void processlcp(int n) {
67+
int N=n-1;
68+
for (int i=0; i<N; i++) lcp[0][i]=getLCP(rankSuf[i], rankSuf[i+1]);
69+
int lgn=0, p2=1;
70+
while (p2<=N) {p2<<=1; lgn++;}
71+
for (int i=1,l=1; i<=lgn; i++,l<<=1) {
72+
for (int j=0; j<n; j++) {
73+
lcp[i][j] = min(lcp[i-1][j], (j+l<N)?lcp[i-1][j+l]:INF);
74+
}
75+
}
76+
}
77+
78+
int frameSize[MAX];
79+
int preprocess(){
80+
for(int i=0, pow2=1; pow2 < MAX; pow2*=2, i++) frameSize[pow2]=i;
81+
for(int i=3;i<MAX;i++) {
82+
if(frameSize[i]==0) {
83+
frameSize[i]=frameSize[i-1];
84+
}
85+
}
86+
}
87+
88+
inline int query(int l, int r){
89+
int p = frameSize[r-l+1];
90+
return min(lcp[p][l], lcp[p][r-(1<<p)+1]);
91+
}
92+
93+
int main() {
94+
string s = "bananan";
95+
int n = s.size();
96+
constructSA(s.c_str(), n);
97+
cout << "---" << endl;
98+
for (int i = 0; i < n; i++) cout << rankSuf[i] << " "; cout << endl;
99+
for (int i = 0; i < n; i++) cout << s.substr(rankSuf[i]) << endl;
100+
101+
cout << getLCP(1, 3) << endl;
102+
103+
processlcp(n);
104+
preprocess();
105+
cout << query(0, 0) << endl;
106+
}

0 commit comments

Comments
(0)

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