-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjitter_search.py
50 lines (43 loc) · 1.27 KB
/
jitter_search.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
##
## jitter search by Jeango
## 2020/3/18 20:42
##
class JitterSearch:
def search(self, s, p):
if len(s)<len(p): return [-1]
j = []
i = 0 # string pointer
for i in range(i, len(s)-len(p)+1):
if p[0] == s[i]:
j.append(i)
jt = len(p) - 1
while jt>0:
for i in range(0, len(j)):
if j[i] < 0: continue
if s[j[i]+jt] != p[jt]:
j[i] = -1
jt -=1
ret = []
for i in j:
if i<0: continue
ret.append(i)
ismain and print(f"""\
P: {p}
S: {s}
at: {"^"*len(p):>{i+len(p)}} pos: {ret}
""")
if len(ret)==0: ret = [-1]
return ret
ismain = __name__ == '__main__'
if ismain:
v = JitterSearch()
v.search(p="llam", s="shellllama")
v.search(p="ma", s="shellllama")
v.search(p="bib", s="bilibili")
v.search(p="ili", s="bilibili")
v.search(p="bibi", s="ilibili")
v.search(p="AAAABAAA",s="AAAABAABAAAABAAABAAAA")
v.search(p="AAAA", s="AAAABAABAAAABAAABAAAA")
v.search(p="loog", s="loon")
v.search(p="loon", s="loon")
v.search(p="loon", s="loo")