Buggy python submission (verdict: Runtime Error):
import sys

N, Q = map(int, sys.stdin.readline().split())
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))


Fixed submission:
# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))

Buggy python submission (verdict: Time Limit Exceeded):
import logging
import sys

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
h = logging.FileHandler("logtest.log")
logger.addHandler(h)

input = sys.stdin.readline

N, Q = map(int, input().split())
logger.info("N: {}, Q: {}".format(N, Q))

s = input()
logger.info("s: {}".format(s))

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

# 二分探索: boundaryを求める
for t, d in reversed(Query):
    logger.info("t: {}, d: {}".format(t, d))
    logger.info("[before] left: {}, right: {}".format(left, right))
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
    logger.info("[after] left: {}, right: {}\n".format(left, right))
print(max(right - left + 1, 0))


Fixed submission:
# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))

Buggy python submission (verdict: Time Limit Exceeded):
import logging
import sys

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
h = logging.FileHandler("logtest.log")
logger.addHandler(h)

# N, Q = map(int, input().split())
input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
logger.info("N: {}, Q: {}".format(N, Q))

s = input()
logger.info("s: {}".format(s))

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

# 二分探索: boundaryを求める
for t, d in reversed(Query):
    logger.info("t: {}, d: {}".format(t, d))
    logger.info("[before] left: {}, right: {}".format(left, right))
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
    logger.info("[after] left: {}, right: {}\n".format(left, right))
print(right - left + 1)


Fixed submission:
# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))

Buggy python submission (verdict: Time Limit Exceeded):
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
h = logging.FileHandler("logtest.log")
logger.addHandler(h)

# N, Q = map(int, input().split())
N, Q = tuple(map(int, input().split()))
logger.info("N: {}, Q: {}".format(N, Q))

s = input()
logger.info("s: {}".format(s))

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

# 二分探索: boundaryを求める
for t, d in reversed(Query):
    logger.info("t: {}, d: {}".format(t, d))
    logger.info("[before] left: {}, right: {}".format(left, right))
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
    logger.info("[after] left: {}, right: {}\n".format(left, right))
print(right - left + 1)


Fixed submission:
# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))

Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))


Fixed submission:
# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))

Buggy python submission (verdict: Time Limit Exceeded):
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
h = logging.FileHandler("logtest.log")
logger.addHandler(h)

N, Q = map(int, input().split())
logger.info("N: {}, Q: {}".format(N, Q))
s = input()
logger.info("s: {}".format(s))

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

# 二分探索: boundaryを求める
for t, d in reversed(Query):
    logger.info("t: {}, d: {}".format(t, d))
    logger.info("[before] left: {}, right: {}".format(left, right))
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
    logger.info("[after] left: {}, right: {}\n".format(left, right))
print(right - left + 1)


Fixed submission:
# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))

Buggy python submission (verdict: Runtime Error):
# import logging

# logger = logging.getLogger(__name__)
# logger.setLevel(logging.INFO)
# h = logging.FileHandler("logtest.log")
# logger.addHandler(h)

N, Q = map(int, input().split())
s = input()

Query = []
for i in range(Q):
    Query.append(input().split())
# logger.info("Query: {}".format(Query))

# left: 左に落ちるゴーレム
# right: 右に落ちるゴーレム
#  N-right-left: 生き残るゴーレム
left, right = 0, N - 1
# logger.info("left: {}, right: {}".format(left, right))

# 二分探索
for ch, way in reversed(Query):
    if way == "L":
        if ch == s[left]:
            left += 1
        if right < N - 1 and ch == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and ch == s[left - 1]:
            left = max(left - 1, 0)
        if ch == s[right]:
            right -= 1
print(max(0, right - left + 1))


Fixed submission:
# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))

Buggy python submission (verdict: Runtime Error):
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
h = logging.FileHandler("logtest.log")
logger.addHandler(h)

N, Q = map(int, input().split())
s = input()

Query = []
for i in range(Q):
    Query.append(input().split())
logger.info("Query: {}".format(Query))

# left: 左に落ちるゴーレム
# right: 右に落ちるゴーレム
#  N-right-left: 生き残るゴーレム
left, right = 0, N - 1
logger.info("left: {}, right: {}".format(left, right))

# 二分探索
for ch, way in reversed(Query):
    if way == "L":
        if ch == s[left]:
            left += 1
        if right < N - 1 and ch == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and ch == s[left - 1]:
            left = max(left - 1, 0)
        if ch == s[right]:
            right -= 1
print(max(0, right - left + 1))


Fixed submission:
# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))

Buggy python submission (verdict: Runtime Error):
# import logging

# logger = logging.getLogger(__name__)
# logger.setLevel(logging.INFO)
# h = logging.FileHandler("logtest.log")
# logger.addHandler(h)

N, Q = map(int, input().split())
S = list(map(str, input().strip()))
# logger.info("S: {}".format(S))

order = [tuple(map(str, input().split())) for _ in range(Q)]
order = order[::-1]
# logger.info("order: {}".format(order))

# left: 左に落ちるゴーレム
# right: 右に落ちるゴーレム
#  N-right-left: 生き残るゴーレム
left, right = 0, N - 1
# logger.info("left: {}, right: {}".format(left, right))

# 二分探索
for a in order:
    t, d = a[0], a[1]
    # logger.info("t: {}, d: {}".format(t, d))
    # logger.info("[before] left: {}, right: {}".format(left, right))

    if d == "L":
        if S[left] == t:
            left += 1
            if left > N - 1:
                pass

        if right < N - 1 and S[right + 1] == t:
            right = min(N - 1, right + 1)

    else:
        if left > 0 and S[left - 1] == t:
            left = max(0, left - 1)

        if S[right] == t:
            right -= 1
            if right < 0:
                pass

    # logger.info("[after] left: {}, right: {}\n".format(left, right))


print(right - left + 1)


Fixed submission:
# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))

Buggy python submission (verdict: Time Limit Exceeded):
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
h = logging.FileHandler("logtest.log")
logger.addHandler(h)

N, Q = map(int, input().split())
S = list(map(str, input().strip()))
logger.info("S: {}".format(S))

order = [tuple(map(str, input().split())) for _ in range(Q)]
order = order[::-1]
logger.info("order: {}".format(order))

# left: 左に落ちるゴーレム
# right: 右に落ちるゴーレム
#  N-right-left: 生き残るゴーレム
left, right = 0, N - 1
logger.info("left: {}, right: {}".format(left, right))

# 二分探索
for a in order:
    t, d = a[0], a[1]
    logger.info("t: {}, d: {}".format(t, d))
    logger.info("[before] left: {}, right: {}".format(left, right))

    if d == "L":
        if S[left] == t:
            left += 1
            if left > N - 1:
                pass

        if right < N - 1 and S[right + 1] == t:
            right = min(N - 1, right + 1)

    else:
        if left > 0 and S[left - 1] == t:
            left = max(0, left - 1)

        if S[right] == t:
            right -= 1
            if right < 0:
                pass

    logger.info("[after] left: {}, right: {}\n".format(left, right))


print(right - left + 1)


Fixed submission:
# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))

Buggy python submission (verdict: Runtime Error):
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
h = logging.FileHandler("logtest.log")
logger.addHandler(h)

N, Q = list(map(int, input().split()))

# ゴーレムのいる位置
# 同じマスに複数いる可能性もあり
s_1 = list(map(str, input()))
s = [[i] for i in s_1]

for i in range(Q):
    # t: name, d: L or R
    t, d = list(map(str, input().split()))
    logger.info("i: {}, t: {}, d: {}".format(i, t, d))
    logger.info("before_s: {}".format(s))

    # tのindex
    t_id_list = []
    for i in range(len(s)):
        if t in s[i]:
            t_id_list.append(i)
    t_id_list = list(set(t_id_list))

    logger.info("t_id_list: {}".format(t_id_list))

    # move
    for t_id in t_id_list:
        logger.info("t_id: {}".format(t_id))
        logger.info("s_now: {}".format(s))

        # 対象のゴーレムtを全て消去する
        for i in range(len(s[t_id])):
            try:
                if s[t_id][i] == t:
                    s[t_id].remove(t)
            except:
                pass
        logger.info("s_remove: {}".format(s))

        if d == "L" and t_id - 1 >= 0:
            s[t_id - 1].append(t)
        elif d == "R" and t_id + 1 <= N:
            s[t_id + 1].append(t)

        logger.info("after_s: {}\n".format(s))

    logger.info("after_s: {}\n".format(s))

cnt = 0
for i in range(len(s)):
    for j in range(len(s[i])):
        if s[i][j] != "":
            cnt += 1
print(cnt)



Fixed submission:
# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))