-
Notifications
You must be signed in to change notification settings - Fork 0
/
suffixArrayApplications.py
102 lines (85 loc) · 2.29 KB
/
suffixArrayApplications.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
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
import tools_karkkainen_sanders as tks
import unittest
def longest(s):
'''
longest repeating substring
'''
sa = tks.simple_kark_sort(s)
lcp = tks.LCP(s, sa)
maxI, maxV = -1, -1
for i, v in enumerate(lcp):
if v > maxV:
maxI, maxV = i, v
return s[sa[maxI]:sa[maxI] + maxV]
def search(P, sStr):
'''
find first substring P
'''
sa = tks.simple_kark_sort(sStr)
m = len(P)
n = len(sStr)
left, right = 0, n # length of sa is n+1
while left < right:
mid = (left + right) >> 1
comp = cmp(sStr[sa[mid]:sa[mid] + m], P)
if comp >= 0:
right = mid
else:
left = mid + 1
if sStr[sa[left]: sa[left] + m] == P:
return sa[left]
else:
return -1
def search2(P, sStr):
'''
find the substring P, all occurances
'''
sa = tks.simple_kark_sort(sStr)
m = len(P)
n = len(sStr)
start, end = -1, -1
# lower bound
left, right = 0, n # length of sa is n+1
while left < right:
mid = (left + right) >> 1
comp = cmp(sStr[sa[mid]:sa[mid] + m], P)
if comp >= 0:
right = mid
else:
left = mid + 1
start = left
if sStr[sa[left]: sa[left] + m] != P:
return []
# upper bound
left, right = 0, n # length of sa is n+1
while left < right:
mid = (left + right) >> 1
comp = cmp(sStr[sa[mid]:sa[mid] + m], P)
if comp > 0:
right = mid
else:
left = mid + 1
end = left
result = [sa[i] for i in range(start, end)]
result.sort()
return result
class testUtility(unittest.TestCase):
def test_longest(self):
s = 'aaaabcaa'
self.assertEquals(longest(s), 'aaa')
def test_search(self):
s = 'a' * 8
occurance = search('ba', s)
self.assertEquals(occurance, -1)
occurance = search('acd', s)
self.assertEquals(occurance, -1)
occurance = search('aaaa', s)
self.assertEquals(occurance, 4)
def test_search2(self):
s = 'a' * 8
occurance = search2('ba', s)
self.assertEquals(occurance, [])
occurance = search2('aaa', s)
self.assertEquals(occurance, range(6))
if __name__ == '__main__':
unittest.main()