Buggy python 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))


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 python 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))


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 python 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))


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 python 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))


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 python submission (verdict: Time Limit Exceeded):
# 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)

golem = [1 for i in range(N)]

positions = {}
for i in range(len(s)):
    if s[i] in positions.keys():
        tmp =  positions[s[i]]
        tmp.append(i)
        positions[s[i]] = tmp
    else:
        positions[s[i]] = [i]
# print(positions)
ans = N

for i in range(Q):
    if T[i] in positions.keys():
        move_poss = positions[T[i]]
        # print(golem)
        if D[i] == "R":
            move_poss.reverse()
            for move in move_poss:
                if move == N-1:
                    ans -= golem[N-1]
                    golem[N-1] = 0
                else:
                    golem[move+1] += golem[move]
                    golem[move] = 0

        else:
            for move in move_poss:
                if move == 0:
                    ans -= golem[0]
                    golem[0] = 0
                else:
                    golem[move-1] += golem[move]
                    golem[move] = 0

print(ans)


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 python submission (verdict: Time Limit Exceeded):
# 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)

golem = [1 for i in range(N)]

positions = {}
for i in range(len(s)):
    if s[i] in positions.keys():
        tmp =  positions[s[i]]
        tmp.append(i)
        positions[s[i]] = tmp
    else:
        positions[s[i]] = [i]
# print(positions)
ans = N

for i in range(Q):
    if T[i] in positions.keys():
        move_poss = positions[T[i]]
        # print(golem)
        if D[i] == "R":
            for j in range(len(move_poss))[::-1]:
                if move_poss[j] == N-1:
                    ans -= golem[N-1]
                    golem[N-1] = 0
                else:
                    golem[move_poss[j]+1] += golem[move_poss[j]]
                    golem[move_poss[j]] = 0

        else:
            for j in range(len(move_poss)):
                if move_poss[j] == 0:
                    ans -= golem[0]
                    golem[0] = 0
                else:
                    golem[move_poss[j]-1] += golem[move_poss[j]]
                    golem[move_poss[j]] = 0

print(ans)


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))