Skip to content

Instantly share code, notes, and snippets.

@fmasanori
Last active September 14, 2018 12:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmasanori/333fde85f447c69dcb34b9b5d7782af7 to your computer and use it in GitHub Desktop.
Save fmasanori/333fde85f447c69dcb34b9b5d7782af7 to your computer and use it in GitHub Desktop.
BooyerMoore
def boyermoore(p, t):
m = len(p)
n = len(t)
if m > n: return -1
pulo = [m for k in range(256)]
for k in range(m - 1):
pulo[ord(p[k])] = m - k - 1
pulo = tuple(pulo)
k = m - 1
while k < n:
j = m - 1
i = k
while j >= 0 and t[i] == p[j]:
j -= 1
i -= 1
if j == -1: return i + 1
k += pulo[ord(t[k])]
return -1
texto = 'os algoritmos de ordenação'
palavra = 'algoritmo'
print (boyermoore(palavra, texto))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment