-
Notifications
You must be signed in to change notification settings - Fork 7
/
substrings.py
47 lines (45 loc) · 1.35 KB
/
substrings.py
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
def make_sa(s):
n = len(s)
sa = list(range(n))
ra = [ord(s[i]) for i in range(n)]
k, maxi = 1, max(300, n)
while k < n:
for kk in [k, 0]:
c = [0]*maxi
for i in range(n): c[ra[i+kk] if i+kk<n else 0] += 1
ss, temp = 0, [0]*n
for i in range(maxi): t = c[i]; c[i] = ss; ss += t
for i in range(n):
idx = ra[sa[i]+kk] if sa[i]+kk < n else 0
temp[c[idx]] = sa[i]
c[idx] += 1
sa = temp
temp, r = [0]*n, 0
temp[sa[0]] = r
for i in range(1, n):
r += ra[sa[i]] != ra[sa[i-1]] or ra[sa[i]+k] != ra[sa[i-1]+k]
temp[sa[i]] = r
ra = temp
if ra[sa[n-1]] == n-1: break
k *= 2
return sa[1:]
def make_lcp(s, sa):
n = len(s)
ra = [0]*n
for i in range(n): ra[sa[i]] = i
k = 0
lcp = [0]*(n-1)
for i in range(n):
if ra[i] == n-1: k = 0; continue
j = sa[ra[i]+1]
while i + k < n and j + k < n and s[i+k] == s[j+k]: k += 1
lcp[ra[i]] = k
if k: k -= 1
return [0]+lcp
for _ in range(int(input())):
s = input(); a = 0
sa = make_sa(s+'\0')
lcp = make_lcp(s, sa)
for i in range(len(lcp)-1):
if lcp[i+1] > lcp[i]: a += lcp[i+1]-lcp[i]
print(a)