Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())
coms = [list(map(str, input().split())) for _ in range(Q)]


def isOK(A, index, coms, c):
    # シミュレーションを行う
    for com in coms:
        if com[0] != A[index]:
            continue
        else:
            if com[1] == 'R':
                if index == len(A)-1:
                    return c == 'R'
                index += 1
            elif com[1] == 'L':
                if index == 0:
                    return c == 'L'
                index -= 1
    return False


def lower_bound(A, coms):
    ng = -1
    ok = len(A)

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if isOK(A, mid, coms, 'R'):
            ok = mid
        else:
            ng = mid
    return ok


def upper_bound(A, coms):
    ng = len(A)
    ok = -1

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if isOK(A, mid, coms, 'L'):
            ok = mid
        else:
            ng = mid
    return ok


lower = lower_bound(s, coms)
upper = upper_bound(s, coms)
print(lower-upper-1)

Fixed submission:
N, Q = map(int, input().split())
s = str(input())
coms = [list(map(str, input().split())) for _ in range(Q)]


def isOK(A, index, coms, c):
    # シミュレーションを行う
    for com in coms:
        if com[0] == A[index]:
            if com[1] == 'R':
                index += 1
                if index == len(A):
                    return c == 'R'
            elif com[1] == 'L':
                index -= 1
                if index == -1:
                    return c == 'L'
    return False


def lower_bound(A, coms):
    ng = -1
    ok = len(A)

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if isOK(A, mid, coms, 'R'):
            ok = mid
        else:
            ng = mid
    return ok


def upper_bound(A, coms):
    ng = len(A)
    ok = -1

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if isOK(A, mid, coms, 'L'):
            ok = mid
        else:
            ng = mid
    return ok


lower = lower_bound(s, coms)
upper = upper_bound(s, coms)
print(lower-upper-1)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())
coms = [list(map(str, input().split())) for _ in range(Q)]


def ok_lower(A, index, coms):
    # シミュレーションを行う
    for com in coms:
        if com[0] == A[index]:
            if com[1] == 'R':
                index += 1
            elif com[1] == 'L':
                index -= 1
            if index == len(A):
                return True
            elif index == -1:
                return False
    return False


def ok_upper(A, index, coms):
    # シミュレーションを行う
    for com in coms:
        if com[0] == A[index]:
            if com[1] == 'R':
                index += 1
            elif com[1] == 'L':
                index -= 1
            if index == len(A):
                return False
            elif index == -1:
                return True
    return False


def lower_bound(A, coms):
    ng = -1
    ok = len(A)

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if ok_lower(A, mid, coms):
            ok = mid
        else:
            ng = mid
    return ok


def upper_bound(A, coms):
    ng = len(A)
    ok = -1

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if ok_upper(A, mid, coms):
            ok = mid
        else:
            ng = mid
    return ok


lower = lower_bound(s, coms)
upper = upper_bound(s, coms)
print(max(0, lower-upper-1))


Fixed submission:
N, Q = map(int, input().split())
s = str(input())
coms = [list(map(str, input().split())) for _ in range(Q)]


def isOK(A, index, coms, c):
    # シミュレーションを行う
    for com in coms:
        if com[0] == A[index]:
            if com[1] == 'R':
                index += 1
                if index == len(A):
                    return c == 'R'
            elif com[1] == 'L':
                index -= 1
                if index == -1:
                    return c == 'L'
    return False


def lower_bound(A, coms):
    ng = -1
    ok = len(A)

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if isOK(A, mid, coms, 'R'):
            ok = mid
        else:
            ng = mid
    return ok


def upper_bound(A, coms):
    ng = len(A)
    ok = -1

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if isOK(A, mid, coms, 'L'):
            ok = mid
        else:
            ng = mid
    return ok


lower = lower_bound(s, coms)
upper = upper_bound(s, coms)
print(lower-upper-1)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())
coms = [list(map(str, input().split())) for _ in range(Q)]

def ok_lower(A, index, coms):
    # シミュレーションを行う
    for com in coms:
        if com[0] == A[index]:
            if com[1] == 'R':
                index += 1
            elif com[1] == 'L':
                index -= 1
        if index == len(A):
            return True
        elif index == -1:
            return False
    return False


def ok_upper(A, index, coms):
    # シミュレーションを行う
    for com in coms:
        if com[0] == A[index]:
            if com[1] == 'R':
                index += 1
            elif com[1] == 'L':
                index -= 1
        if index == len(A):
            return False
        elif index == -1:
            return True
    return False


def lower_bound(A, coms):
    ng = -1
    ok = len(A)

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if ok_lower(A, mid, coms):
            ok = mid
        else:
            ng = mid
    return ok


def upper_bound(A, coms):
    ng = len(A)
    ok = -1

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if ok_upper(A, mid, coms):
            ok = mid
        else:
            ng = mid
    return ok


lower = lower_bound(s, coms)
upper = upper_bound(s, coms)
print(max(0, lower-upper-1))

Fixed submission:
N, Q = map(int, input().split())
s = str(input())
coms = [list(map(str, input().split())) for _ in range(Q)]


def isOK(A, index, coms, c):
    # シミュレーションを行う
    for com in coms:
        if com[0] == A[index]:
            if com[1] == 'R':
                index += 1
                if index == len(A):
                    return c == 'R'
            elif com[1] == 'L':
                index -= 1
                if index == -1:
                    return c == 'L'
    return False


def lower_bound(A, coms):
    ng = -1
    ok = len(A)

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if isOK(A, mid, coms, 'R'):
            ok = mid
        else:
            ng = mid
    return ok


def upper_bound(A, coms):
    ng = len(A)
    ok = -1

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if isOK(A, mid, coms, 'L'):
            ok = mid
        else:
            ng = mid
    return ok


lower = lower_bound(s, coms)
upper = upper_bound(s, coms)
print(lower-upper-1)

Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
s = str(input())
s_inv = s[::-1]
coms = [list(map(str, input().split())) for _ in range(Q)]
# 後ろから見る
coms = coms[::-1]
i = 0
j = 0
for com in coms:
    # Rに対する処理
    if com[1] == 'R' and com[0] == s_inv[i]:
        if i <= N-1:
            i = i+1
    # Lに対する処理
    elif com[1] == 'L' and com[0] == s[j]:
        if j <= N-1:
            j = j+1
print(max(N-i-j, 0))

Fixed submission:
N, Q = map(int, input().split())
s = str(input())
coms = [list(map(str, input().split())) for _ in range(Q)]


def isOK(A, index, coms, c):
    # シミュレーションを行う
    for com in coms:
        if com[0] == A[index]:
            if com[1] == 'R':
                index += 1
                if index == len(A):
                    return c == 'R'
            elif com[1] == 'L':
                index -= 1
                if index == -1:
                    return c == 'L'
    return False


def lower_bound(A, coms):
    ng = -1
    ok = len(A)

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if isOK(A, mid, coms, 'R'):
            ok = mid
        else:
            ng = mid
    return ok


def upper_bound(A, coms):
    ng = len(A)
    ok = -1

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if isOK(A, mid, coms, 'L'):
            ok = mid
        else:
            ng = mid
    return ok


lower = lower_bound(s, coms)
upper = upper_bound(s, coms)
print(lower-upper-1)

Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
s = str(input())
s_inv = s[::-1]
coms = [list(map(str, input().split())) for _ in range(Q)]
# 後ろから見る
coms = coms[::-1]
i = 0
j = 0
for com in coms:
    # Rに対する処理
    if com[1] == 'R' and com[0] == s_inv[i]:
        i += 1
    # Lに対する処理
    elif com[1] == 'L' and com[0] == s[j]:
        j += 1
print(N-i-j)

Fixed submission:
N, Q = map(int, input().split())
s = str(input())
coms = [list(map(str, input().split())) for _ in range(Q)]


def isOK(A, index, coms, c):
    # シミュレーションを行う
    for com in coms:
        if com[0] == A[index]:
            if com[1] == 'R':
                index += 1
                if index == len(A):
                    return c == 'R'
            elif com[1] == 'L':
                index -= 1
                if index == -1:
                    return c == 'L'
    return False


def lower_bound(A, coms):
    ng = -1
    ok = len(A)

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if isOK(A, mid, coms, 'R'):
            ok = mid
        else:
            ng = mid
    return ok


def upper_bound(A, coms):
    ng = len(A)
    ok = -1

    while(abs(ok-ng) > 1):
        mid = (ok + ng) // 2

        if isOK(A, mid, coms, 'L'):
            ok = mid
        else:
            ng = mid
    return ok


lower = lower_bound(s, coms)
upper = upper_bound(s, coms)
print(lower-upper-1)