Problem p03081 (python)

Fixed submission:
# encoding:utf-8
import copy
import numpy as np
import random
import bisect #bisect_left　これで二部探索の大小検索が行える

N,Q = map(int, input().split())
s = input()
td = [input().split() for i in range(Q)]

td.reverse()

nl = 0
nr = N-1
L = []
R = []
for i in td:#全ての指示
    # print(L,R)
    if i[1]=="L":
        if nl < N:
            if s[nl] == i[0]:
                L.append(i[0])
                nl += 1
            if len(R) != 0:
                if R[-1]==i[0]:
                    R.pop()
                    nr += 1
    else:
        if nr >= 0:
          if s[nr] == i[0]:
            R.append(i[0])
            nr -= 1
        if len(L) != 0:
          if L[-1] == i[0]:
            L.pop()
            nl -= 1

print(N-len(L)-len(R))

Buggy submission (verdict: Runtime Error):
# encoding:utf-8
import copy
import numpy as np
import random
import bisect #bisect_left　これで二部探索の大小検索が行える

N,Q = map(int, input().split())
s = str(input())
T = []
D = []
for i in range(Q):
    t,d = map(str,input().split())
    T.append(t)
    D.append(d)

T.reverse()
D.reverse()

nl = 0
nr = N-1
L = []#回収できた分
R = []
for i in range(Q):
    if D[i]=="R":
        if s[nr] == T[i]:
            L.append(s[nr])
            nr -= 1
        elif len(R)>0:
            if R[-1] == T[i]:
                R.pop()
                nl += 1

    else:
        if s[nl] == T[i]:
            R.append(s[nl])
            nl += 1
        elif len(L)>0:
            if L[-1] == T[i]:
                L.pop()
                nr -= 1

print(N-len(R)-len(L))



#
# print(N-len(L)-len(R))

Buggy submission (verdict: Runtime Error):
# encoding:utf-8
import copy
import numpy as np
import random
import bisect #bisect_left　これで二部探索の大小検索が行える

N,Q = map(int, input().split())
s = str(input())
T = []
D = []
for i in range(Q):
    t,d = map(str,input().split())
    T.append(t)
    D.append(d)

T.reverse()
D.reverse()

nl = 0
nr = N-1
L = []#回収できた分
R = []
for i in range(Q):
    if D[i]=="R":
        if s[nr] == T[i]:
            L.append(s[nr])
            nr -= 1
        elif len(R)>0:
            if R[-1] == T[i]:
                R.pop()
                nr += 1

    else:
        if s[nl] == T[i]:
            R.append(s[nl])
            nl += 1
        elif len(L)>0:
            if L[-1] == T[i]:
                L.pop()
                nl -= 1

print(N-len(R)-len(L))



#
# print(N-len(L)-len(R))

Buggy submission (verdict: Runtime Error):
# encoding:utf-8
import copy
import numpy as np
import random
import bisect #bisect_left　これで二部探索の大小検索が行える

N,Q = map(int, input().split())
s = str(input())
T = []
D = []
for i in range(Q):
    t,d = map(str,input().split())
    T.append(t)
    D.append(d)

T.reverse()
D.reverse()

nl = 0
nr = N-1
L = []#回収できた分
R = []
for i in range(Q):
    if D[i]=="R":
        if s[nr] == T[i]:
            L.append(s[nr])
            nr -= 1
        elif len(R)>0:
            if R[-1] == s[nr]:
                R.pop()
                nr += 1

    else:
        if s[nl] == T[i]:
            R.append(s[nl])
            nl += 1
        elif len(L)>0:
            if L[-1] == s[nl]:
                L.pop()
                nl -= 1

print(N-len(R)-len(L))



#
# print(N-len(L)-len(R))

Buggy submission (verdict: Runtime Error):
# encoding:utf-8
import copy
import numpy as np
import random
import bisect #bisect_left　これで二部探索の大小検索が行える

N,Q = map(int, input().split())
s = str(input())
T = []
D = []
for i in range(Q):
    t,d = map(str,input().split())
    T.append(t)
    D.append(d)

T.reverse()
D.reverse()

nl = 0
nr = N-1
L = []#回収できた分
R = []
for i in range(Q):
    if D[i]=="R":
        if s[nr] == T[i]:
            L.append(s[nr])
            nr -= 1
        elif len(R)>0:
            if R[-1] == s[nr]:
                R.pop()

    else:
        if s[nl] == T[i]:
            R.append(s[nl])
            nl += 1
        elif len(L)>0:
            if L[-1] == s[nl]:
                L.pop()

print(N-len(R)-len(L))



#
# print(N-len(L)-len(R))