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

query = [list(input().split()) for _ in range(q)]

# 左に落ちる (1から, leftで判断(leftは落ちる個数))
left = 0
right = n+1
while right - left > 1:
    mid = (left + right) // 2
    index = mid - 1
    for w, dir in query:
        if s[index] != w:
            continue
        if dir == 'R':
            index += 1
        elif dir == 'L':
            index -= 1

        if index == n:
            right = mid
            break
        elif index == -1:
            left = mid
            break
    if index != -1:
        right = mid
c1 = left

# 右 (leftで判断(leftが落ちないギリギリ。最大はn-1, 最小は-1))
left = -1
right = n+1
while right - left > 1:
    mid = (left + right) // 2
    index = mid
    for w, dir in query:
        if s[index] != w:
            continue
        if dir == 'R':
            index += 1
        elif dir == 'L':
            index -= 1

        if index == n:
            right = mid
            break
        elif index == -1:
            left = mid
            break
    if index != n:
        left = mid

c2 = n - 1 - left

print(n - c1 - c2)


Fixed submission:
n, q = map(int, input().split())
s = input()

query = [list(input().split()) for _ in range(q)]

# 左に落ちる。leftから左(leftを含む)は左に落ちて、rightから右は左に落ちない。
left = -1
right = n
while right - left > 1:
    mid = (left + right) // 2
    index = mid
    for w, dir in query:
        if s[index] != w:
            continue
        if dir == 'R':
            index += 1
        elif dir == 'L':
            index -= 1

        if index == n:
            right = mid
            break
        elif index == -1:
            left = mid
            break
    if index != -1:
        right = mid
c1 = left + 1

# 右に落ちる。leftから左(leftを含む)は右に落ちず、rightから右は右に落ちる。
left = -1
right = n
while right - left > 1:
    mid = (left + right) // 2
    index = mid
    for w, dir in query:
        if s[index] != w:
            continue
        if dir == 'R':
            index += 1
        elif dir == 'L':
            index -= 1

        if index == n:
            right = mid
            break
        elif index == -1:
            left = mid
            break
    if index != n:
        left = mid

c2 = n - 1 - left

print(n - c1 - c2)

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()

query = [list(input().split()) for _ in range(q)]

if n == 1:
    for w, dir in query:
        if w == s:
            print(0)
            exit()
    print(1)
    exit()
# 左に落ちる (1から, leftで判断(leftは落ちる個数))
left = 0
right = n
while right - left > 1:
    mid = (left + right) // 2
    index = mid - 1
    for w, dir in query:
        if s[index] != w:
            continue
        if dir == 'R':
            index += 1
        elif dir == 'L':
            index -= 1

        if index == n:
            right = mid
            break
        elif index == -1:
            left = mid
            break
    if index != -1:
        right = mid
c1 = left

# 右 (leftで判断(leftが落ちないギリギリ。最大はn-1, 最小は-1))
left = -1
right = n
while right - left > 1:
    mid = (left + right) // 2
    index = mid
    for w, dir in query:
        if s[index] != w:
            continue
        if dir == 'R':
            index += 1
        elif dir == 'L':
            index -= 1

        if index == n:
            right = mid
            break
        elif index == -1:
            left = mid
            break
    if index != n:
        left = mid

c2 = n - 1 - left

print(n - c1 - c2)


Fixed submission:
n, q = map(int, input().split())
s = input()

query = [list(input().split()) for _ in range(q)]

# 左に落ちる。leftから左(leftを含む)は左に落ちて、rightから右は左に落ちない。
left = -1
right = n
while right - left > 1:
    mid = (left + right) // 2
    index = mid
    for w, dir in query:
        if s[index] != w:
            continue
        if dir == 'R':
            index += 1
        elif dir == 'L':
            index -= 1

        if index == n:
            right = mid
            break
        elif index == -1:
            left = mid
            break
    if index != -1:
        right = mid
c1 = left + 1

# 右に落ちる。leftから左(leftを含む)は右に落ちず、rightから右は右に落ちる。
left = -1
right = n
while right - left > 1:
    mid = (left + right) // 2
    index = mid
    for w, dir in query:
        if s[index] != w:
            continue
        if dir == 'R':
            index += 1
        elif dir == 'L':
            index -= 1

        if index == n:
            right = mid
            break
        elif index == -1:
            left = mid
            break
    if index != n:
        left = mid

c2 = n - 1 - left

print(n - c1 - c2)