Problem p03081 (python)

Fixed submission:
import sys
input = sys.stdin.readline

N,Q=map(int,input().split())
S=input().strip()
M=[input().split() for i in range(Q)]

PLACE=-1

for i in range(Q-1,-1,-1):

    if PLACE+1<N and M[i][0]==S[PLACE+1] and M[i][1]=="L":
        PLACE+=1

    elif PLACE>=0 and M[i][0]==S[PLACE] and M[i][1]=="R":
        PLACE-=1

    if PLACE==N:
        print(0)
        sys.exit()

PLACE2=N

for i in range(Q-1,-1,-1):

    if PLACE2-1>=0 and M[i][0]==S[PLACE2-1] and M[i][1]=="R":
        PLACE2-=1

    elif PLACE2<=N-1 and M[i][0]==S[PLACE2] and M[i][1]=="L":
        PLACE2+=1

    if PLACE==-1:
        print(0)
        sys.exit()

print(max(0,N-(PLACE+1+(N-PLACE2))))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N,Q=map(int,input().split())
S=input().strip()
M=[input().split() for i in range(Q)]

PLACE=-1

for i in range(Q-1,-1,-1):

    if M[i][0]==S[PLACE+1] and M[i][1]=="L":
        PLACE+=1

    elif PLACE>=0 and M[i][0]==S[PLACE] and M[i][1]=="R":
        PLACE-=1

    if PLACE==N:
        print(0)
        sys.exit()

PLACE2=N

for i in range(Q-1,-1,-1):

    if M[i][0]==S[PLACE2-1] and M[i][1]=="R":
        PLACE2-=1

    elif PLACE2<=N-1 and M[i][0]==S[PLACE2] and M[i][1]=="L":
        PLACE2+=1

    if PLACE==-1:
        print(0)
        sys.exit()

print(max(0,N-(PLACE+1+(N-PLACE2))))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
import bisect
sys.setrecursionlimit(10**8)

N,Q=map(int,input().split())
S=input().strip()
M=[input().split() for i in range(Q)]

#逆からシミューレション

PLACE=0
flag=0

for now in range(Q-1,-1,-1):
    if M[now][0]==S[PLACE] and M[now][1]=="L":
        PLACE+=1

PLACE2=N-1

for now in range(Q-1,-1,-1):
    if M[now][0]==S[PLACE2] and M[now][1]=="R":
        PLACE2-=1


if PLACE-1>PLACE2+1:
    print(N)
else:
    print(N-(PLACE+(N-1-PLACE2)))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
import bisect

N,Q=map(int,input().split())
S=input().strip()
S=[ord(s)-65 for s in S]
M=[input().split() for i in range(Q)]

MLIST=[[] for i in range(26)]

for i in range(Q):
    m,lr=M[i][0],M[i][1]
    MLIST[ord(m)-65].append(i)

dist=dict()
def distinct(PLACE,TURN):
    #print(PLACE,TURN)

    x=bisect.bisect_left(MLIST[S[PLACE]],TURN)

    if x==len(MLIST[S[PLACE]]):
        #dist[(PLACE,TURN)]=1
        return 1

    TURN=MLIST[S[PLACE]][x]

    if dist.get((PLACE,TURN))!=None:
        return dist.get((PLACE,TURN))
    
    if PLACE==0 and M[TURN][1]=="L":
        dist[(PLACE,TURN)]=0
        return 0
    
    if PLACE==N-1 and M[TURN][1]=="R":
        dist[(PLACE,TURN)]=0
        return 0

    if M[TURN][1]=="L":
        dist[(PLACE,TURN)]=distinct(PLACE-1,TURN+1)
        return dist[(PLACE,TURN)]

    if M[TURN][1]=="R":
        dist[(PLACE,TURN)]=distinct(PLACE+1,TURN+1)
        return dist[(PLACE,TURN)]

ANS=0
for i in range(N):
    ANS+=distinct(i,0)
    #print(i,dist)

print(ANS)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
import bisect

N,Q=map(int,input().split())
S=input().strip()
S=[ord(s)-65 for s in S]
M=[input().split() for i in range(Q)]

MLIST=[[] for i in range(26)]

for i in range(Q):
    m,lr=M[i][0],M[i][1]
    MLIST[ord(m)-65].append(i)

dist=dict()
def distinct(PLACE,TURN):

    x=bisect.bisect_left(MLIST[S[PLACE]],TURN)

    if x==len(MLIST[S[PLACE]]):
        dist[(PLACE,TURN)]=0
        return 1

    TURN=MLIST[S[PLACE]][x]

    if dist.get((PLACE,TURN))!=None:
        return dist.get((PLACE,TURN))
    
    if PLACE==0 and M[TURN][1]=="L":
        dist[(PLACE,TURN)]=0
        return 0
    
    if PLACE==N-1 and M[TURN][1]=="R":
        dist[(PLACE,TURN)]=0
        return 0

    if M[TURN][1]=="L":
        return distinct(PLACE-1,TURN+1)

    if M[TURN][1]=="R":
        return distinct(PLACE+1,TURN+1)

ANS=0
for i in range(N):
    ANS+=distinct(i,0)

print(ANS)