Here is the working version of your code:
def Rabin_Karp_Matcher(text, pattern, d, q):
n = len(text)
m = len(pattern)
h = pow(d,m-1)%q
p = 0
t = 0
result = []
for i in range(m):
p = (d*p+ord(pattern[i]))%q
t = (d*t+ord(text[i]))%q
for s in range(n-m+1):
if p == t:
match = True
for i in range(m):
if pattern[i] != text[s+i]:
match = False
break
if match:
result = result + [s]
if s < n-m:
t = (t-h*ord(text[s]))%q
t = (t*d+ord(text[s+m]))%q
t = (t+q)%q
return result
print (Rabin_Karp_Matcher ("3141592653589793", "26", 257, 11))
print (Rabin_Karp_Matcher ("xxxxx", "xx", 40999999, 999999937))
Output:
[6]
[0, 1, 2, 3]
, text[0..m] == pattern. , text[1..m+1] == pattern. , text[0] ( h): t = (t-h*ord(text[s]))%q. text[m] : t = (t*d+ord(text[s+m]))%q. text[1] text[m+1] .. t = (t+q)%q , q (-q; 0], , [0; q).
, n-m+1, n-m, - for s in range(n-m+1). ( "xx" "xxxxx" ).
:
h = pow(d,m-1)%q , m . q m-2.
O (nm) . text="a"*100000 pattern="a"*50000 50001 , , -. , , (.. , ). q .