-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSuffix_Array.cpp
More file actions
165 lines (141 loc) · 4.18 KB
/
Suffix_Array.cpp
File metadata and controls
165 lines (141 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<bits/stdc++.h>
using namespace std;
const int N = 100007;
char str[100001];
struct SUFFIX_ARRAY
{
int wa[N],wb[N],ws[N],wv[N],r[N];
int len; // length of the string
int rank[N]; // here rank said in which position a suffix is situated in the sorted suffix list.
int height[N];
//Height gives the lcp of sa[i] ans sa[i-1] So index height[1] contains the lcp of sa[1] and and sa[0]
int sa[N]; //sa gives the sorted suffix indexes (staring index)//Index zero te always strlen tomo adress thake
// abc
//er jonno 3 0 1 2
int dp[N][21]; // for LCP
int cmp(int *ar,int a,int b,int l)
{
return (ar[a]==ar[b]) && (ar[a+l]==ar[b+l]);
}
void DA(int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0; i<m; i++)
ws[i]=0;
for(i=0; i<n; i++)
ws[x[i]=r[i]]++;
for(i=1; i<m; i++)
ws[i]+=ws[i-1];
for(i=n-1; i>=0; i--)
sa[--ws[x[i]]]=i;
for(j=1,p=1; p<n; j*=2,m=p)
{
for(p=0,i=n-j; i<n; i++)
y[p++]=i;
for(i=0; i<n; i++)
if(sa[i]>=j)
y[p++]=sa[i]-j;
for(i=0; i<n; i++)
wv[i]=x[y[i]];
for(i=0; i<m; i++)
ws[i]=0;
for(i=0; i<n; i++)
ws[wv[i]]++;
for(i=1; i<m; i++)
ws[i]+=ws[i-1];
for(i=n-1; i>=0; i--)
sa[--ws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
//printf("p = %d\n", p );
}
}
void calheight(int n)
{
// memset(height,0,sizeof(height));
// memset(rank,0,sizeof(rank));
int i,j,k=0;
for(i=1; i<=n; i++)
rank[sa[i]]=i;
for(i=0; i<n; height[rank[i++]] = k )
for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
}
void generate(char *s)
{
int n;
for (n=0; s[n] ; n++)
r[n]=(int)s[n];
r[n]=0;
this->len = n;
DA(n+1,128);
calheight(n);
}
// create sparse table in height array
void sparse_table()
{
int n = len+1;
for(int i=0; i<n; i++)
dp[i][0]=height[i];
for (int j=1; (1 << j) <= n; j++)
for (int i = 0; i+(1 << j)-1<n; i++)
dp[i][j] = (dp[i][j-1]<=dp[ i+(1<<(j-1)) ][j-1] )? dp[i][j-1]:dp[i+(1<<(j-1))][j-1];
}
// start/end , the suffix that situated at start/end index after suffix sort
// RMQ(find minimum value)
int find_lcp(int start,int end)
{
if(start>end)
swap(start,end);
start++;
int diff=end-start;
diff=31 - __builtin_clz(diff+1);
return dp[start][diff]<=dp[end-(1<<diff)+1][diff]?dp[start][diff]:dp[end-(1<<diff)+1][diff];
}
// find lcp of s and e th suffix of original string
int get_lcp(int s,int e)
{
return find_lcp( rank[s],rank[e] );
}
void print()
{
cout << "length : " << len << endl;
cout << "s[n] = '$' " << endl;
cout << "sorted suffix index(0 based) : " << endl;
for(int i=0; i<=len; i++)
{
// for(int )
printf("%d\n",sa[i]);
}
cout << endl;
cout << "LCP between i and i-1 " << endl;
for(int i=0; i<=len; i++) cout << height[i] << " , ";
cout << endl;
cout << "rank of suffix " << endl;
for(int i=0; i<=len; i++)
cout << rank[i] << " , ";
cout << endl;
long long nn,sum=0;
// cout << "LCP between i and i-1 " << endl;
for(int i=1; i<=len; i++)
sum+=height[i];
cout<<"sum="<<sum<<endl;
nn=(len*(len+1))/2;
printf("%lld\n",nn-sum);
}
} SA;
int main()
{
//http://codeforces.com/contest/611/submission/15270024
int i,j,n,t;
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
SA.generate(str);//Send o indexed string
SA.sparse_table();
// int x=SA.get_lcp(0,3);//gives the lcp of position 0,and 3
SA.print();
}
//hicout<<x<<endl;
return 0;
}