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 c7b5d87

Browse files
Minor bugs fixed
1 parent 4780016 commit c7b5d87

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

‎src/untested-codes/suffix_array.cpp

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,17 @@
1212
#include <queue>
1313
using namespace std;
1414

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;
15+
//----------------------
2516
const int INF = 1000000000+5;
26-
const int MAX = 200005;
17+
18+
// inputs
19+
string s;
20+
int n;
2721

2822
int lgn=0;
2923
int sa[25][1000005];
3024
int rankSuf[1000005];
31-
void constructSA(const char s[], int n) {
32-
cout << s << endl;
25+
void constructSA() {
3326
map<int,int> rank;
3427
for (int i=0; i<n; i++) rank[s[i]]=0;
3528
int ctr=1;
@@ -58,12 +51,13 @@ int getLCP(int p, int q) {
5851
if (sa[i][p]==sa[i][q]) {
5952
l+=len; p+=len, q+=len;
6053
}
54+
if (p>=n || q>=n) break;
6155
}
6256
return l;
6357
}
6458

6559
int lcp[25][1000005];
66-
void processlcp(int n) {
60+
void processlcp() {
6761
int N=n-1;
6862
for (int i=0; i<N; i++) lcp[0][i]=getLCP(rankSuf[i], rankSuf[i+1]);
6963
int lgn=0, p2=1;
@@ -75,10 +69,10 @@ void processlcp(int n) {
7569
}
7670
}
7771

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++) {
72+
int frameSize[1000005];
73+
int processFrameSize(){
74+
for(int i=0, pow2=1; pow2<1000005; pow2*=2, i++) frameSize[pow2]=i;
75+
for(int i=3;i<1000005;i++) {
8276
if(frameSize[i]==0) {
8377
frameSize[i]=frameSize[i-1];
8478
}
@@ -89,18 +83,19 @@ inline int query(int l, int r){
8983
int p = frameSize[r-l+1];
9084
return min(lcp[p][l], lcp[p][r-(1<<p)+1]);
9185
}
86+
//---------------------
9287

9388
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;
89+
s = "banana";
90+
n = s.size();
91+
92+
constructSA();
9993
for (int i = 0; i < n; i++) cout << s.substr(rankSuf[i]) << endl;
10094

10195
cout << getLCP(1, 3) << endl;
10296

103-
processlcp(n);
104-
preprocess();
105-
cout << query(0, 0) << endl;
97+
processlcp();
98+
processFrameSize();
99+
100+
cout << query(1, 1) << endl;
106101
}

0 commit comments

Comments
(0)

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