目录
1.826. 单链表 - AcWing题库
模拟链表
N=105
ne=[0 for i in range(N)]
e=[0 for i in range(N)]
head=-1
idx=0
def init():
head=-1
idx=0
def add_head(x):
global idx
global head
e[idx]=x
ne[idx]=head
head=idx
idx+=1
return
def remove(k):
ne[k]=ne[ne[k]]
return
def add(k,x):
global idx
e[idx]=x
ne[idx]=ne[k]
ne[k]=idx
idx+=1
return
m=int(input())
for _ in range(m):
martix=list(input().split())
if martix[0]=='H':
add_head(int(martix[1]))
elif martix[0]=='I':
add(int(martix[1])-1,int(martix[2]))
else:
if not int(martix[1]):
head=ne[head]
else:
remove(int(martix[1])-1)
i=head
while i!=-1:
print(e[i],end=" ")
i=ne[i]
2.单调栈
n=int(input())
martix=list(map(int,input().split()))
s=[]
for i in martix:
while s and s[-1]>=i:
s.pop()
if s:
print(s[-1],end=" ")
else:
print(-1,end=" ")
s.append(i)
3.154. 滑动窗口 - AcWing题库
单调队列
n,k=map(int,input().split())
a=list(map(int,input().split()))
from collections import deque
q=deque([])
for i in range(n):
if q and i-k+1>q[0]:
q.popleft()
while q and a[q[-1]]>=a[i]:
q.pop()
q.append(i)
if i>=k-1:
print(a[q[0]],end=" ")
print()
q=deque([])
for i in range(n):
if q and i-k+1>q[0]:
q.popleft()
while q and a[q[-1]]<=a[i]:
q.pop()
q.append(i)
if i>=k-1:
print(a[q[0]],end=" ")
4.KMP
# 3
# aba
# 5
# ababa
N=10010
ne=[0 for i in range(N)]
n=int(input())
p=" "+input()
m=int(input())
s=" "+input()
j=0
for i in range(2,n+1):
while j and p[i]!=p[j+1]:
j=ne[j]
if p[i]==p[j+1]:
j+=1
ne[i]=j
j=0
for i in range(1,m+1):
while j and s[i]!=p[j+1]:
j=ne[j]
if s[i]==p[j+1]:
j+=1
if j==n:
print(i-n)
j=ne[j]