def getHash(l, r): # 通过6/11
"""手动实现hash函数"""
l %= mod
r %= mod
return h[r] - h[l-1]*p[r-l+1]
if __name__=="__main__":
n, m = map(int, input().split())
s = " "
s+=input().strip()
mod = 1<<64
# 生成p数组,用来保存p^x,其中p^0=1
N = 100010
P = 131
p = [0]*(N) # p^0=1
p[0] = 1
# 初始化h数组,用来存储字符串前缀的哈希值
h = [0]*(N)
for i in range(1, n+1):
# print(i)
p[i] = p[i-1]*P
h[i] = h[i-1]*P+ ord(s[i])
for i in range(m):
l1, r1, l2, r2 = map(int, input().split())
if getHash(l1, r1)==getHash(l2, r2): print("Yes")
else: print("No")
if __name__=="__main__": # AC 8/11
n, m = map(int, input().split())
s = " "
s+=input().strip()
for i in range(m):
l1, r1, l2, r2 = map(int, input().split())
# if getHash(l1, r1)==getHash(l2, r2): print("Yes")
if hash(s[l1:r1+1])==hash(s[l2:r2+1]): print("Yes") # 使用python标准哈希函数
else: print("No")
def get(l, r):
return (h[r] - h[l - 1] * p[r - l + 1]) % Q
n, m = map(int, input().split())
string = ‘ ‘ + str(input())
N = 100010
P = 131
p = [0] * N
h = [0] * N
Q = 2 ** 64
p[0] = 1
for i in range(1, n + 1):
p[i] = (p[i - 1] * P) % Q
h[i] = (h[i - 1] * P + ord(string[i])) % Q
while m:
l1, r1, l2, r2 = map(int, input().split())
if get(l1, r1) == get(l2, r2):
print(‘Yes’)
else:
print(‘No’)
m -= 1
AC了。结果要mod
菜鸡一枚,我也不清楚啊,可以问问y总(苦笑)
大佬好,我也是写Python,也是TLE。。是不是数据量对Python不友好的问题?谢谢大佬解答~!
def get(l, r):
return (h[r] - h[l - 1] * p[r - l + 1]) % Q
n, m = map(int, input().split())
string = ‘ ‘ + str(input())
N = 100010
P = 131
p = [0] * N
h = [0] * N
Q = 2 ** 64
p[0] = 1
for i in range(1, n + 1):
p[i] = (p[i - 1] * P) % Q
h[i] = (h[i - 1] * P + ord(string[i])) % Q
while m:
l1, r1, l2, r2 = map(int, input().split())
if get(l1, r1) == get(l2, r2):
print(‘Yes’)
else:
print(‘No’)
m -= 1
结果要mod
厉害,学习了~!谢谢