Problem p03081 (python)

Fixed submission:
N,Q = map(int,input().split())
s = input()
q = [[0,0] for i in range(Q)]
for i in range(Q):
    q[i] = input().split()
    
def L(n):
    pos = n
    for i in range(Q):
        t,d = q[i]
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            elif d == 'R':
                pos += 1
            if pos == -1:
                return True
            if pos == N:
                return False
    return False

def R(n):
    pos = n
    for i in range(Q):
        t,d = q[i]
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            elif d == 'R':
                pos += 1
            if pos == N:
                return True
            if pos == -1:
                return False
    return False

n_mid = N//2
n_l = 0
n_r = N-1
for i in range(25):
    if L(n_mid):
        n_l = n_mid
        n_mid = (n_l+n_r)//2
    else:
        n_r = n_mid
        n_mid = (n_l+n_r)//2

if not L(n_l):
    l = -1
elif L(n_r):
    l = n_r
else:
    l = n_l

n_mid = N//2-1
n_l = 0
n_r = N-1
for i in range(25):
    if R(n_mid):
        n_r = n_mid
        n_mid = (n_l+n_r)//2
    else:
        n_l = n_mid
        n_mid = (n_l+n_r)//2

if not R(n_r):
    r = N
elif R(n_l):
    r = n_l
else:
    r = n_r

ans = 0
for i in range(N):
    if l < i < r:
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
s = input()
if s.count('R') > s.count('B'):
    print('Yes')
else:
    print('No')

#C
#%%
N,Q = map(int,input().split())
s = input()
q = [[0,0] for i in range(Q)]
for i in range(Q):
    q[i] = input().split()
    
def L(n):
    pos = n
    for i in range(Q):
        t,d = q[i]
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            elif d == 'R':
                pos += 1
            if pos == -1:
                return True
            if pos == N:
                return False
    return False

def R(n):
    pos = n
    for i in range(Q):
        t,d = q[i]
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            elif d == 'R':
                pos += 1
            if pos == N:
                return True
            if pos == -1:
                return False
    return False

n_mid = N//2
n_l = 0
n_r = N-1
for i in range(25):
    if L(n_mid):
        n_l = n_mid
        n_mid = (n_l+n_r)//2
    else:
        n_r = n_mid
        n_mid = (n_l+n_r)//2

if not L(n_l):
    l = -1
elif L(n_r):
    l = n_r
else:
    l = n_l

n_mid = N//2-1
n_l = 0
n_r = N-1
for i in range(25):
    if R(n_mid):
        n_r = n_mid
        n_mid = (n_l+n_r)//2
    else:
        n_l = n_mid
        n_mid = (n_l+n_r)//2

if not R(n_r):
    r = N
elif R(n_l):
    r = n_l
else:
    r = n_r

ans = 0
for i in range(N):
    if l < i < r:
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
s = input()
q = [[0,0] for i in range(Q)]
for i in range(Q):
    q[i] = input().split()
    
def L(n):
    pos = n
    for i in range(Q):
        t,d = q[i]
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            elif d == 'R':
                pos += 1
            if pos == -1:
                return True
    return False

def R(n):
    pos = n
    for i in range(Q):
        t,d = q[i]
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            elif d == 'R':
                pos += 1
            if pos == N:
                return True
    return False

n_mid = N//2
n_l = 0
n_r = N-1
for i in range(25):
    if L(n_mid):
        n_l = n_mid
        n_mid = (n_l+n_r)//2
    else:
        n_r = n_mid
        n_mid = (n_l+n_r)//2

if not L(n_l):
    l = -1
elif L(n_r):
    l = n_r
else:
    l = n_l

n_mid = N//2-1
n_l = 0
n_r = N-1
for i in range(25):
    if R(n_mid):
        n_r = n_mid
        n_mid = (n_l+n_r)//2
    else:
        n_l = n_mid
        n_mid = (n_l+n_r)//2

if not R(n_r):
    r = N
elif R(n_l):
    r = n_l
else:
    r = n_r

ans = 0
for i in range(N):
    if l < i < r:
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
s = input()
q = [[0,0] for i in range(Q)]
for i in range(Q):
    q[i] = input().split()
    
def L(n):
    pos = n
    for i in range(Q):
        t,d = q[i]
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            elif d == 'R':
                pos += 1
            if pos == -1:
                return True
    return False

def R(n):
    pos = n
    for i in range(Q):
        t,d = q[i]
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            elif d == 'R':
                pos += 1
            if pos == N:
                return True
    return False

n_mid = N//2-1
n_l = 0
n_r = N-1
for i in range(25):
    if L(n_mid):
        n_l = n_mid
        n_mid = (n_l+n_r)//2
    else:
        n_r = n_mid
        n_mid = (n_l+n_r)//2

if not L(n_l):
    l = -1
elif L(n_r):
    l = n_r
else:
    l = n_l

n_mid = N//2-1
n_l = 0
n_r = N-1
for i in range(25):
    if R(n_mid):
        n_r = n_mid
        n_mid = (n_l+n_r)//2
    else:
        n_l = n_mid
        n_mid = (n_l+n_r)//2

if not R(n_r):
    r = N
elif R(n_l):
    r = n_l
else:
    r = n_r

ans = 0
for i in range(N):
    if l < i < r:
        ans += 1
print(ans)