You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.6 KiB
54 lines
1.6 KiB
from typing import Dict
|
|
|
|
|
|
def make_km_table(pattern: str) -> Dict[str, int]:
|
|
table = dict()
|
|
raise Exception("TODO")
|
|
return table
|
|
|
|
|
|
class Bm(object):
|
|
def __init__(self, text: str, pattern: str):
|
|
self.text = text
|
|
self.pattern = pattern
|
|
# self.table = make_km_table(pattern)
|
|
|
|
def decide_slide_width(self, c: str) -> int:
|
|
assert len(c) == 1
|
|
raise Exception("TODO")
|
|
return -1
|
|
|
|
def search(self) -> int:
|
|
PATTERN_LENGTH = len(self.pattern)
|
|
TEXT_LENGTH = len(self.text)
|
|
|
|
END_OF_PATTERN = PATTERN_LENGTH - 1
|
|
LAST_CHAR_OF_PATTERN = self.pattern[-1]
|
|
|
|
offset = {c: PATTERN_LENGTH - i -1 for i, c in enumerate(self.pattern[:-1])}
|
|
print(offset)
|
|
|
|
matches = []
|
|
head = END_OF_PATTERN
|
|
|
|
while head < TEXT_LENGTH:
|
|
print(f'{" " * (head - PATTERN_LENGTH + 1)}{self.pattern}')
|
|
print(self.text[:head + 1])
|
|
print(f'{" " * head}^')
|
|
|
|
if self.text[head] == LAST_CHAR_OF_PATTERN:
|
|
for i, c in enumerate(reversed(self.pattern)):
|
|
# print(text[head - i], c)
|
|
if self.text[head - i] != c:
|
|
head += offset[self.text[head]]
|
|
break
|
|
else:
|
|
print("!!!")
|
|
return head - PATTERN_LENGTH + 1
|
|
else:
|
|
try:
|
|
head += offset[self.text[head]]
|
|
except KeyError:
|
|
head += PATTERN_LENGTH
|
|
print('----------------------------------')
|
|
return -1
|