Problem p03081 (python)

Fixed submission:
N,Q=map(int,input().split())
s=["x"]+list(input())+["x"]
#print(s)
L=[]
for i in range(Q):
    t=list(input().split())
    L.append(t)
#print(L)
h=N
l=1
LEFT=-10
RIGHT=-10
while abs(h-l)>2:
    Mid=(h+l)//2
    P=Mid
    for i in range(Q):
        if s[P]==L[i][0]:
            if L[i][1]=="L":
                P-=1
            else:
                P+=1
                
    if P==0:
        l=Mid
    else:
        h=Mid
#print(l,h)
for j in range(4):
    M=l-1+j
    P=M
    for i in range(Q):
        if s[P]==L[i][0]:
            #print("S",j,P,i)
            if L[i][1]=="R":
                P+=1
            else:
                P-=1
    #print("A",M,P)
    if P!=0:
        LEFT=l-1+j
        break

h=N
l=1

while abs(h-l)>2:
    Mid=(h+l)//2
    P=Mid
    for i in range(Q):
        if s[P]==L[i][0]:
            if L[i][1]=="L":
                P-=1
            else:
                P+=1
                
    if P==N+1:
        h=Mid
    else:
        l=Mid
for j in range(4):
    M=h+1-j
    P=M
    for i in range(Q):
        if s[P]==L[i][0]:
            if L[i][1]=="L":
                P-=1
            else:
                P+=1
    if P!=N+1:
        RIGHT=h+1-j
        break
if LEFT==-10 or RIGHT==-10:
    print(0)
else:
    print(max(0,RIGHT - LEFT +1))
#print(RIGHT,LEFT)

Buggy submission (verdict: Runtime Error):
N,Q=map(int,input().split())
s=["x"]+list(input())+["x"]
#print(s)
L=[]
for i in range(Q):
    t=list(input().split())
    L.append(t)
#print(L)
h=N
l=1

while abs(h-l)>2:
    Mid=(h+l)//2
    P=Mid
    for i in range(Q):
        if s[P]==L[i][0]:
            if L[i][1]=="L":
                P-=1
            else:
                P+=1
                
    if P==0:
        l=Mid
    else:
        h=Mid
#print(l,h)
for j in range(4):
    M=l-1+j
    P=M
    for i in range(Q):
        if s[P]==L[i][0]:
            #print("S",j,P,i)
            if L[i][1]=="R":
                P+=1
            else:
                P-=1
    #print("A",M,P)
    if P!=0:
        LEFT=l-1+j
        break

h=N
l=1

while abs(h-l)>2:
    Mid=(h+l)//2
    P=Mid
    for i in range(Q):
        if s[P]==L[i][0]:
            if L[i][1]=="L":
                P-=1
            else:
                P+=1
                
    if P==N+1:
        h=Mid
    else:
        l=Mid
for j in range(4):
    M=h+1-j
    P=M
    for i in range(Q):
        if s[P]==L[i][0]:
            if L[i][1]=="L":
                P-=1
            else:
                P+=1
    if P!=N+1:
        RIGHT=h+1-j
        break
        
print(max(0,RIGHT - LEFT +1))
#print(RIGHT,LEFT)

Buggy submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
N,Q=map(int,input().split())
s=["s"]+list(input())+["e"]

A=[]

for i in range(Q):
    t,d=list(input().split())
    A.append([t,d])
#print(A)

High=N
Low=1

while abs(High - Low)>4:
    Mid=(High + Low)//2
    #print(H,Mid,L)
    P=Mid
    T=s[P]
    for i in range(Q):
        if A[i][0]==T:
            if A[i][1]=="L":
                P-=1
                T=s[P]
            else:
                P+=1
                T=s[P]
    if P==0:
        Low=Mid
    else:
        High=Mid
for j in range(min(5,N)):
    Mid= High-j
    if Mid==0:
        left=0
    P=Mid
    T=s[P]
    for i in range(Q):
        if A[i][0]==T:
            if A[i][1]=="L":
                P-=1
                T=s[P]
            else:
                P+=1
                T=s[P]
    if P==0:
        left=Mid+1
        break
#print(left)        

High=N
Low=1

while abs(High - Low)>4:
    Mid=(High + Low)//2
    #print(H,Mid,L)
    P=Mid
    T=s[P]
    for i in range(Q):
        if A[i][0]==T:
            if A[i][1]=="L":
                P-=1
                T=s[P]
            else:
                P+=1
                T=s[P]
    if P==N:
        High=Mid
    else:
        Low=Mid
for j in range(min(5,N)):
    Mid= Low+j
    if Mid==N:
        right=N
    P=Mid
    T=s[P]
    for i in range(Q):
        if A[i][0]==T:
            if A[i][1]=="L":
                P-=1
                T=s[P]
            else:
                P+=1
                T=s[P]
    if P==N+1:
        right=Mid-1
        break
#print(right)        
print(max(0,right-left+1))