Buggy python submission (verdict: Time Limit Exceeded):
def simulate(n):
    if n==-1: return 0
    if n==N: return 2
    
    for i in range(Q):
        if TD[i][0]==s[n]:
            if TD[i][1]=="R": n+=1
            if TD[i][1]=="L": n-=1

        if n<0 : return 0
        elif n>=N: return 2 
    return 1

N, Q = map(int, input().split())
s = input()
TD = [input().split() for i in range(Q)]
memo=[-1]*(N+2)


low = 0
high =N-1
# t は中央番目の数
t =int((low+high)/2) 

# 探索の下限のlowが上限のhighになるまで探索
# lowがhighに達すると数は見つからなかったということ
while (1):
    if memo[t]==-1: memo[t]=simulate(t)
    if memo[t+1]==-1: memo[t+1]=simulate(t+1)
    
    if memo[t] in [0,1] and memo[t+1]==2:
        break
    elif memo[t] in [0,1]:
        low = t+1
    elif memo[t]==2:
        high = t-1
    t = int((low + high) / 2)


R=t

low = 0
high =N-1
# t は中央番目の数
t =int((low+high)/2) 

while (1):
    if memo[t]==-1: memo[t]=simulate(t)
    if memo[t-1]==-1: memo[t-1]=simulate(t-1)
    
    if memo[t-1]==0 and memo[t] in [1,2]:
        break
    elif memo[t]==0:
        low = t+1
    elif memo[t] in [1,2]:
        high = t-1
    t = int((low + high) / 2)

L=t

print(R-L+1)

Fixed submission:
N,Q=map(int,input().split())
s="0"+input()+"0"
queri=[list(input().split()) for i in range(Q)]

L,R=0,N+1
for i in range(Q-1,-1,-1):
    t,d=queri[i]
    if t==s[L] and d=="R":
        L-=1
    elif t==s[L+1] and d=="L":
        L+=1
    if t==s[R] and d=="L":
        R+=1
    elif t==s[R-1] and d=="R":
        R-=1

print(R-L-1)

Buggy python submission (verdict: Runtime Error):
def simulate(n):
    p=n
    if memo[n]!=-1: return memo[n]
    if n==-1: return 0
    if n==N: return 2
    
    for i in range(Q):
        if TD[i][0]==s[n]:
            if TD[i][1]=="R": n+=1
            if TD[i][1]=="L": n-=1

        if n<0 :
            memo[p]=0
            return 0
        elif n>=N:
            memo[p]=2
            return 2 
    memo[p]=1
    return 1

N, Q = map(int, input().split())
s = input()
TD = [input().split() for i in range(Q)]
memo=[-1]*N


low = 0
high =N-1
# t は中央番目の数
t =int((low+high)/2) 

# 探索の下限のlowが上限のhighになるまで探索
# lowがhighに達すると数は見つからなかったということ
while (1):
    if simulate(t) in [0,1] and simulate(t+1)==2:
        break
    elif simulate(t) in [0,1]:
        low = t+1
    elif simulate(t)==2:
        high = t-1
    t = int((low + high) / 2)


R=t

low = 0
high =N-1
# t は中央番目の数
t =int((low+high)/2) 

while (1):
    if simulate(t-1)==0 and simulate(t) in [1,2]:
        break
    elif simulate(t)==0:
        low = t+1
    elif simulate(t) in [1,2]:
        high = t-1
    t = int((low + high) / 2)

L=t

print(R-L+1)
print(memo)

Fixed submission:
N,Q=map(int,input().split())
s="0"+input()+"0"
queri=[list(input().split()) for i in range(Q)]

L,R=0,N+1
for i in range(Q-1,-1,-1):
    t,d=queri[i]
    if t==s[L] and d=="R":
        L-=1
    elif t==s[L+1] and d=="L":
        L+=1
    if t==s[R] and d=="L":
        R+=1
    elif t==s[R-1] and d=="R":
        R-=1

print(R-L-1)

Buggy python submission (verdict: Time Limit Exceeded):
def simulate(n):
    if n==-1: return 0
    if n==N: return 2
    
    for i in range(Q):
        if TD[i][0]==s[n]:
            if TD[i][1]=="R": n+=1
            if TD[i][1]=="L": n-=1

        if n<0 : return 0
        elif n>=N: return 2 
    return 1

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

low = 0
high =N-1
# t は中央番目の数
t =int((low+high)/2) 

# 探索の下限のlowが上限のhighになるまで探索
# lowがhighに達すると数は見つからなかったということ
while (1):
    if simulate(t) in [0,1] and simulate(t+1)==2:
        break
    elif simulate(t) in [0,1]:
        low = t+1
    elif simulate(t)==2:
        high = t-1
    t = int((low + high) / 2)


R=t

low = 0
high =N-1
# t は中央番目の数
t =int((low+high)/2) 

while (1):
    if simulate(t-1)==0 and simulate(t) in [1,2]:
        break
    elif simulate(t)==0:
        low = t+1
    elif simulate(t) in [1,2]:
        high = t-1
    t = int((low + high) / 2)

L=t

print(R-L+1)

Fixed submission:
N,Q=map(int,input().split())
s="0"+input()+"0"
queri=[list(input().split()) for i in range(Q)]

L,R=0,N+1
for i in range(Q-1,-1,-1):
    t,d=queri[i]
    if t==s[L] and d=="R":
        L-=1
    elif t==s[L+1] and d=="L":
        L+=1
    if t==s[R] and d=="L":
        R+=1
    elif t==s[R-1] and d=="R":
        R-=1

print(R-L-1)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q=map(int,input().split())
chr=input()

s=[-1]+[ord(chr[i])-ord("A")+1 for i in range(N)]+[-1]
state=[0]+[1 for i in range(N)]+[0]

for i in range(Q):
    a,b=input().split()
    k=ord(a)-ord("A")+1
    for j in range(1,N+1):
        if s[j]==k:
            if b=="R": state[j+1]+=state[j]
            if b=="L": state[j-1]+=state[j]
            state[j]=0

zanki=N-state[0]-state[N+1]

print(zanki)

Fixed submission:
N,Q=map(int,input().split())
s="0"+input()+"0"
queri=[list(input().split()) for i in range(Q)]

L,R=0,N+1
for i in range(Q-1,-1,-1):
    t,d=queri[i]
    if t==s[L] and d=="R":
        L-=1
    elif t==s[L+1] and d=="L":
        L+=1
    if t==s[R] and d=="L":
        R+=1
    elif t==s[R-1] and d=="R":
        R-=1

print(R-L-1)