Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. def phash(s):
  2. h=[0]
  3. b=37
  4. M=(1<<64)-1
  5. for c in s:
  6. h.append((h[-1]*b+ord(c))%M)
  7. return h
  8.  
  9.  
  10. def rabin_karp(s, t, table):
  11. b=37
  12. M=(1<<64)-1
  13. m=len(s)
  14. n=len(t)
  15. s=phash(s)
  16. t=phash(t)[-1]
  17. R=b**n%M
  18. for i in range(m-n+1):
  19. if t==(s[i+n]-s[i]*R)%M:
  20. table.append(i)
  21. return table
  22.  
  23. table=[]
  24. s=input()
  25. t=input()
  26. table=rabin_karp(t, s, table)
  27. if len(table)!=0:
  28. print(*table)
  29. else:
  30. print('-1')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement