问题描述
#python3
import sys
from _collections import deque
def hashTable(s,prime,x): # calcuate hash
h = list([] for _ in range(len(s) + 1))
h[0] = 0
for i in range(1,len(s) + 1):
h[i] = (h[i - 1] * x + ord(s[i - 1])) % prime
return h
def subhash(table,start,length): #calculate subhash
y = pow(x,length,pr)
hash_value = (table[start + length] - y * table[start]) % pr
return hash_value
def nextmsm(le,i,n): #find next mismatch
c = 0
st = deque()
st.append((0,le))
a = 0
while len(st) != 0:
if c > n:
break
a,le = st.pop()
th = subhash(tx,a+i,le)
ph = subhash(pt,a,le)
if th == ph:
continue
if le == 1:
if th != ph:
c = c + 1
else :
st.append((a,le//2))
st.append((a+le//2,le - le//2))
if c > n:
return False
return True
def solve(o,text,pattern): # find pattern with mismatch
global tx,pt
pos = []
tx = hashTable(text,pr,x)
pt = hashTable(pattern,x)
for i in range(len(text) - len(pattern)+1):
if nextmsm(len(pattern),o):
pos.append(i)
return pos
pr = 1000000007
x = 263
while True:
line = input()
k,t,p = line.split()
ans = solve(int(k),p)
print(len(ans),*ans)
这个 sol 首先接受输入 k 是最大不匹配,t= 文本,p = 模式,然后传递给计算哈希值的求解函数,并调用函数 msmatch 来查找不匹配。 Msmatach 函数计算 subhash,然后在二进制搜索的帮助下帮助找到不匹配
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)