-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBoyerMoore.py
More file actions
34 lines (28 loc) · 877 Bytes
/
BoyerMoore.py
File metadata and controls
34 lines (28 loc) · 877 Bytes
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
from collections import defaultdict
def BoyerMoore(s: str, t: str) -> bool:
'''文字列 s の中に t が存在するかを判別する'''
skip_table = defaultdict(lambda : -1)
for i, j in enumerate(t[::-1]):
if skip_table[j] == -1:
skip_table[j] = i
l_s = len(s)
l_t = len(t)
if l_s < l_t:
return False
else:
i = l_t - 1
cnt = 0
while i < l_s:
if s[i] == t[-1 - cnt]:
cnt += 1
i -= 1
else:
i += l_t if skip_table[s[i]] == -1 else skip_table[s[i]]
cnt = 0
if cnt == l_t:
print(f'[{i + 1}, {i + l_t}] の区間に {t} が見つかりました。')
return True
return False
s = 'abcdefghijklmnopqrstuvwxyz'
t = 'def'
print(BoyerMoore(s, t))