Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def phash(s):
- h=[0]
- b=37
- M=(1<<64)-1
- for c in s:
- h.append((h[-1]*b+ord(c))%M)
- return h
- def rabin_karp(s, t, table):
- b=37
- M=(1<<64)-1
- m=len(s)
- n=len(t)
- s=phash(s)
- t=phash(t)[-1]
- R=b**n%M
- for i in range(m-n+1):
- if t==(s[i+n]-s[i]*R)%M:
- table.append(i)
- return table
- table=[]
- s=input()
- t=input()
- table=rabin_karp(t, s, table)
- if len(table)!=0:
- print(*table)
- else:
- print('-1')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement