12
12
#include < queue>
13
13
using namespace std ;
14
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 ;
15
+ // ----------------------
25
16
const int INF = 1000000000 +5 ;
26
- const int MAX = 200005 ;
17
+
18
+ // inputs
19
+ string s;
20
+ int n;
27
21
28
22
int lgn=0 ;
29
23
int sa[25 ][1000005 ];
30
24
int rankSuf[1000005 ];
31
- void constructSA (const char s[], int n) {
32
- cout << s << endl;
25
+ void constructSA () {
33
26
map<int ,int > rank;
34
27
for (int i=0 ; i<n; i++) rank[s[i]]=0 ;
35
28
int ctr=1 ;
@@ -58,12 +51,13 @@ int getLCP(int p, int q) {
58
51
if (sa[i][p]==sa[i][q]) {
59
52
l+=len; p+=len, q+=len;
60
53
}
54
+ if (p>=n || q>=n) break ;
61
55
}
62
56
return l;
63
57
}
64
58
65
59
int lcp[25 ][1000005 ];
66
- void processlcp (int n ) {
60
+ void processlcp () {
67
61
int N=n-1 ;
68
62
for (int i=0 ; i<N; i++) lcp[0 ][i]=getLCP (rankSuf[i], rankSuf[i+1 ]);
69
63
int lgn=0 , p2=1 ;
@@ -75,10 +69,10 @@ void processlcp(int n) {
75
69
}
76
70
}
77
71
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++) {
82
76
if (frameSize[i]==0 ) {
83
77
frameSize[i]=frameSize[i-1 ];
84
78
}
@@ -89,18 +83,19 @@ inline int query(int l, int r){
89
83
int p = frameSize[r-l+1 ];
90
84
return min (lcp[p][l], lcp[p][r-(1 <<p)+1 ]);
91
85
}
86
+ // ---------------------
92
87
93
88
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 ();
99
93
for (int i = 0 ; i < n; i++) cout << s.substr (rankSuf[i]) << endl;
100
94
101
95
cout << getLCP (1 , 3 ) << endl;
102
96
103
- processlcp (n);
104
- preprocess ();
105
- cout << query (0 , 0 ) << endl;
97
+ processlcp ();
98
+ processFrameSize ();
99
+
100
+ cout << query (1 , 1 ) << endl;
106
101
}
0 commit comments