-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDISSUBSTR.cpp
More file actions
114 lines (112 loc) · 2.42 KB
/
DISSUBSTR.cpp
File metadata and controls
114 lines (112 loc) · 2.42 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
#include <bits/stdc++.h>
using namespace std;
#define MAX 50009
#define PB push_back
#define MP make_pair
int rank[20][MAX];
struct tuple
{
int pos;
int firstHalf,secondHalf;
};
bool compare(const tuple &a , const tuple &b)
{
return a.firstHalf == b.firstHalf ?a.secondHalf < b.secondHalf :a.firstHalf < b.firstHalf;
}
void counting_sort(tuple t[],int n)
{
int count[MAX];
tuple temp[n+9];
memset(count , 0, sizeof count);
for(int i = 0 ; i < n ; i++)
count[t[i].secondHalf + 1] ++;
for(int i = 1 ; i < MAX ; i++)
count[i] += count[i-1];
for(int i = 0 ; i < n ; i++)
{
temp[count[t[i].secondHalf+1] - 1] = t[i];
count[t[i].secondHalf+1]--;
}
memset(count , 0 , sizeof count);
for(int i = 0 ; i < n ; i++)
count[t[i].firstHalf +1]++;
for(int i = 1; i < MAX ; i ++)
count[i] += count[i-1];
for(int i = n-1 ; i >=0 ; i--){
t[count[temp[i].firstHalf+1 ] - 1] = temp[i];
count[temp[i].firstHalf+1 ]--;
}
}
int * suffix_array(tuple t[] , char s[], int length )
{
int pos = 0;
int *arr = (int*)calloc(length+9,sizeof(int));
for(int i=0;i<length;i++)
rank[0][i] = s[i] - 'A';
for(int cnt = 1,stp = 1;(cnt>>1) < length ; cnt<<=1,stp++)
{
for(int i = 0;i<length ; i++)
{
t[i].firstHalf = rank[stp-1][i];
t[i].secondHalf = i+cnt < length ? rank[stp-1][i+cnt] : -1;
t[i].pos = i;
}
//sort(t,t+length,compare);
counting_sort(t,length);
int rnk = 0;
for(int i = 0 ; i<length ; i++)
{
if((i > 0) && (t[i-1].firstHalf == t[i].firstHalf && t[i].secondHalf == t[i-1].secondHalf))
rnk = rank[stp][t[i-1].pos];
else
rnk = i;
rank[stp][t[i].pos] = rnk;
}
}
pos = ceil(log(length)/log(2));
for(int i=0;i<length;i++)
arr[rank[pos][i]] = i;
return arr;
}
int LCP(int i,int j,int n)
{
int res = 0;
if(i==j)
return n - i;
for(int stp = ceil(log(n)/log(2)) ; stp>=0 && i < n && j < n ; stp--)
if(rank[stp][i] == rank[stp][j])
{
res += 1<<stp;
i += 1<<stp;
j += 1<<stp;
}
return res;
}
int LCParray(char s[],int p[],int n)
{
int sum = 0;
for(int i = 1 ; i < n ; i++)
sum += (LCP(p[i-1],p[i],n));
return sum;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char s[50009];
scanf("%s",s);
int n = strlen(s);
//memset(rank,0,sizeof rank);
tuple t[n + 9];
int *p;
p = suffix_array(t,s,n);
long long lcp_sum = 0,suffix_sum = 0;
lcp_sum = LCParray(s,p,n);
for(int i=0;i<n;i++)
suffix_sum += p[i];
printf("%lld\n",(long long)n*n - lcp_sum - suffix_sum);
}
return 0;
}