Buggy python submission (verdict: Runtime Error):
n, q = map(int, input().split())
s = input()
dic = {}
t_list = []
d_list = []

for i in range(q):
    t, d = input().split()
    t_list.append(t)
    if d == 'L':
        d_list.append(-1)
    else:
        d_list.append(1)


def find_pos(left, right):
    return left + (right - left - 1) // 2


def calc(sites, ts, ds, _n, _q):
    def check(idx):
        num = idx
        for j in range(_q):
            site = sites[num]
            if ts[j] == site:
                num += ds[j]
            if num < 0:
                return False
        return True

    def func(left, right):
        assert left > -1
        assert right < _n + 1
        if right - left == 1:
            if check(left):
                return left
            else:
                return right
        pos = find_pos(left, right)
        if check(pos):
            return func(left, pos + 1)
        else:
            return func(pos + 1, right)
    return func


f1 = calc(s, t_list, d_list, n, q)
f2 = calc(s[::-1], t_list, [-i*1 for i in d_list], n, q)
print(n - f1(0, n) - f2(0, n))


Fixed submission:
n, q = map(int, input().split())
s = input()
dic = {}
t_list = []
d_list = []

for i in range(q):
    t, d = input().split()
    t_list.append(t)
    if d == 'L':
        d_list.append(-1)
    else:
        d_list.append(1)


def find_pos(left, right):
    return left + (right - left - 1) // 2


def calc(sites, ts, ds):
    def check(idx):
        num = idx
        for j in range(q):
            site = sites[num]
            if ts[j] == site:
                num += ds[j]
            if num < 0:
                return -1
            if num > n - 1:
                return 1
        return 0

    def func(left, right):
        assert left > -1
        assert right < n + 1
        if right - left == 1:
            if check(left) > -1:
                return left
            else:
                return right
        pos = find_pos(left, right)
        if check(pos) > -1:
            return func(left, pos + 1)
        else:
            return func(pos + 1, right)
    return func


f1 = calc(s, t_list, d_list)
f2 = calc(s[::-1], t_list, [-i*1 for i in d_list])
print(n - f1(0, n) - f2(0, n))

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000)

n, q = map(int, input().split())
s = input()
dic = {}
t_list = []
d_list = []

for i in range(q):
    t, d = input().split()
    t_list.append(t)
    if d == 'L':
        d_list.append(-1)
    else:
        d_list.append(1)


def find_pos(left, right):
    return left + (right - left - 1) // 2


def calc(sites, ts, ds):
    def check(idx):
        num = idx
        for j in range(q):
            site = sites[num]
            if ts[j] == site:
                num += ds[j]
            if num < 0:
                return -1
            if num > n - 1:
                return 1
        return 0

    def func(left, right):
        if right - left == 1:
            if check(right) > -1:
                return left
            else:
                return right
        pos = find_pos(left, right)
        if check(pos) > -1:
            return func(left, pos + 1)
        else:
            return func(pos + 1, right)
    return func


f1 = calc(s, t_list, d_list)
f2 = calc(s[::-1], t_list, [-i*1 for i in d_list])
left_out = f1(0, n)
right_out = f2(0, n)
print(n - left_out - right_out)


Fixed submission:
n, q = map(int, input().split())
s = input()
dic = {}
t_list = []
d_list = []

for i in range(q):
    t, d = input().split()
    t_list.append(t)
    if d == 'L':
        d_list.append(-1)
    else:
        d_list.append(1)


def find_pos(left, right):
    return left + (right - left - 1) // 2


def calc(sites, ts, ds):
    def check(idx):
        num = idx
        for j in range(q):
            site = sites[num]
            if ts[j] == site:
                num += ds[j]
            if num < 0:
                return -1
            if num > n - 1:
                return 1
        return 0

    def func(left, right):
        assert left > -1
        assert right < n + 1
        if right - left == 1:
            if check(left) > -1:
                return left
            else:
                return right
        pos = find_pos(left, right)
        if check(pos) > -1:
            return func(left, pos + 1)
        else:
            return func(pos + 1, right)
    return func


f1 = calc(s, t_list, d_list)
f2 = calc(s[::-1], t_list, [-i*1 for i in d_list])
print(n - f1(0, n) - f2(0, n))

Buggy python submission (verdict: Runtime Error):
import numpy as np
n, q = map(int, input().split())
s = input()
dic = {}
g = np.ones(n).astype(int)
t_list = []
d_list = []

for i in range(q):
    t, d = input().split()
    t_list.append(t)
    if d == 'L':
        d_list.append(-1)
    else:
        d_list.append(1)


def find_pos(left, right):
    return left + (right - left - 1) // 2


def calc(sites, ts, ds):
    def check(idx):
        num = idx
        for j in range(q):
            site = sites[num]
            if ts[j] == site:
                num += ds[j]
            if num < 0:
                return -1
            if num > n - 1:
                return 1
        return 0

    def func(left, right):
        if right - left == 1:
            if check(right) > -1:
                return left
            else:
                return right
        pos = find_pos(left, right)
        if check(pos) > -1:
            return func(left, pos + 1)
        else:
            return func(pos + 1, right)
    return func


f1 = calc(s, t_list, d_list)
f2 = calc(s[::-1], t_list, [-i*1 for i in d_list])
left_out = f1(0, n)
right_out = f2(0, n)
print(n - left_out - right_out)


Fixed submission:
n, q = map(int, input().split())
s = input()
dic = {}
t_list = []
d_list = []

for i in range(q):
    t, d = input().split()
    t_list.append(t)
    if d == 'L':
        d_list.append(-1)
    else:
        d_list.append(1)


def find_pos(left, right):
    return left + (right - left - 1) // 2


def calc(sites, ts, ds):
    def check(idx):
        num = idx
        for j in range(q):
            site = sites[num]
            if ts[j] == site:
                num += ds[j]
            if num < 0:
                return -1
            if num > n - 1:
                return 1
        return 0

    def func(left, right):
        assert left > -1
        assert right < n + 1
        if right - left == 1:
            if check(left) > -1:
                return left
            else:
                return right
        pos = find_pos(left, right)
        if check(pos) > -1:
            return func(left, pos + 1)
        else:
            return func(pos + 1, right)
    return func


f1 = calc(s, t_list, d_list)
f2 = calc(s[::-1], t_list, [-i*1 for i in d_list])
print(n - f1(0, n) - f2(0, n))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n, q = map(int, input().split())
s = input()
dic = {}
g = np.ones(n).astype(int)
for i, c in enumerate(s):
    if c in dic.keys():
        dic[c].append(i)
    else:
        dic[c] = [i]

for i in range(q):
    t, d = input().split()
    sign = 0
    if d == 'L':
        sign = -1
    else:
        sign = 1
    try:
        for m in dic[t]:
            number = g[m]
            g[m] = 0
            if 0 <= m + sign < n:
                g[m + sign] += number
    except:
        continue
print(int(sum(g)))


Fixed submission:
n, q = map(int, input().split())
s = input()
dic = {}
t_list = []
d_list = []

for i in range(q):
    t, d = input().split()
    t_list.append(t)
    if d == 'L':
        d_list.append(-1)
    else:
        d_list.append(1)


def find_pos(left, right):
    return left + (right - left - 1) // 2


def calc(sites, ts, ds):
    def check(idx):
        num = idx
        for j in range(q):
            site = sites[num]
            if ts[j] == site:
                num += ds[j]
            if num < 0:
                return -1
            if num > n - 1:
                return 1
        return 0

    def func(left, right):
        assert left > -1
        assert right < n + 1
        if right - left == 1:
            if check(left) > -1:
                return left
            else:
                return right
        pos = find_pos(left, right)
        if check(pos) > -1:
            return func(left, pos + 1)
        else:
            return func(pos + 1, right)
    return func


f1 = calc(s, t_list, d_list)
f2 = calc(s[::-1], t_list, [-i*1 for i in d_list])
print(n - f1(0, n) - f2(0, n))

Buggy python submission (verdict: Time Limit Exceeded):
import time
import numpy as np
start = time.time()
n, q = map(int, input().split())
s = input()
dic = {}
g = np.ones(n).astype(int)
for i, c in enumerate(s):
    if c in dic.keys():
        dic[c].append(i)
    else:
        dic[c] = [i]

for i in range(q):
    t, d = input().split()
    sign = 0
    if t not in dic.keys():
        continue
    if d == 'L':
        sign = -1
    else:
        sign = 1
    for m in dic[t]:
        number = g[m]
        g[m] = 0
        if 0 <= m + sign < n:
            g[m + sign] += number

print(int(sum(g)))


Fixed submission:
n, q = map(int, input().split())
s = input()
dic = {}
t_list = []
d_list = []

for i in range(q):
    t, d = input().split()
    t_list.append(t)
    if d == 'L':
        d_list.append(-1)
    else:
        d_list.append(1)


def find_pos(left, right):
    return left + (right - left - 1) // 2


def calc(sites, ts, ds):
    def check(idx):
        num = idx
        for j in range(q):
            site = sites[num]
            if ts[j] == site:
                num += ds[j]
            if num < 0:
                return -1
            if num > n - 1:
                return 1
        return 0

    def func(left, right):
        assert left > -1
        assert right < n + 1
        if right - left == 1:
            if check(left) > -1:
                return left
            else:
                return right
        pos = find_pos(left, right)
        if check(pos) > -1:
            return func(left, pos + 1)
        else:
            return func(pos + 1, right)
    return func


f1 = calc(s, t_list, d_list)
f2 = calc(s[::-1], t_list, [-i*1 for i in d_list])
print(n - f1(0, n) - f2(0, n))