Skip to content

Instantly share code, notes, and snippets.

@eloidrai
Created December 18, 2022 13:49
Show Gist options
  • Save eloidrai/b860db429291f0eecf8c0556645507de to your computer and use it in GitHub Desktop.
Save eloidrai/b860db429291f0eecf8c0556645507de to your computer and use it in GitHub Desktop.
from collections import defaultdict
def generer_tableau(mot):
d = defaultdict(lambda : -1)
for i in range(len(mot)):
for j in range(i+1, len(mot)):
d[(mot[i], j)] = i
return d
def boyer_moore(t, m):
dico = generer_tableau(m)
i = 0
while i <= len(t) - len(m):
for j in range(len(m)-1, -1, -1):
if t[i+j] != m[j]:
break
else:
print("Occurence trouvée en position", i)
i += j - dico[(t[i+j], j)]
boyer_moore("désencastrement", "en")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment