滑动窗口
还可以
# 单调减-找最大值
n,k = map(int,input().split())
nums = list(map(int,input().split()))
N = 1000010
q = [0]*N
hh,tt = 0,-1
q=[0]*N
for i in range(n):
if tt>=hh and i-q[hh]+1>k:
hh += 1
while tt>=hh and nums[i]<=nums[q[tt]]:
tt -= 1
tt += 1
q[tt] = i
if i-k+1>=0:
print(nums[q[hh]], end = ' ')
print()
hh,tt = 0,-1
for i in range(n):
if tt>=hh and i-q[hh]+1>k:
hh += 1
while tt>=hh and nums[i]>=nums[q[tt]]:
tt -= 1
tt += 1
q[tt] = i
if i-k+1>=0:
print(nums[q[hh]], end = ' ')
KMP
生成next数组时,加1之后再赋值。
n = int(input())
p = ' '+ input()
m = int(input())
s = ' '+ input()
N = 100010
ne = [0]*N
j = 0
for i in range(2,n+1):
while j and p[j+1]!=p[i]:
j = ne[j]
if p[j+1]==p[i]:
j += 1
# 加1之后再赋值
ne[i] = j
j = 0
for i in range(1,m+1):
while j and p[j+1]!=s[i]:
j = ne[j]
if p[j+1]==s[i]:
j +=1
if j==n:
print(i-j, end=' ')
j = ne[j]