Skip to content

Instantly share code, notes, and snippets.

View itanfeng's full-sized avatar

Feng Tan itanfeng

  • Sichuan University
  • Chengdu
View GitHub Profile
@itanfeng
itanfeng / boyer-moore.py
Created June 13, 2022 06:32 — forked from dbrgn/boyer-moore.py
Boyer-Moore-Algorithm in Python
class last_occurrence(object):
"""Last occurrence functor."""
def __init__(self, pattern, alphabet):
"""Generate a dictionary with the last occurrence of each alphabet
letter inside the pattern.
Note: This function uses str.rfind, which already is a pattern
matching algorithm. There are more 'basic' ways to generate this
dictionary."""
def turbo_bmh(string, pattern):
"""
Bad character heuristic, good suffix heuristic, turbo-shift heuristic implemented on Python
"""
def _suffices_preprocessing(suffix):
suffix[m - 1] = m
g = m - 1
for i in range(m - 2, -1, -1):
if i > g and suffix[i + m - f - 1] < i - g: