Buggy python submission (verdict: Time Limit Exceeded):

#通るかな？
def 解():
    import numpy as np
    import sys
    input = sys.stdin.readline

    N,Q = [int(_) for _ in input().split()]

    S = input()
    dS = {}
    for i in range(1,N+1):
        s = S[i-1]
        if s in dS:
            dS[s].add(i)
        else:
            dS[s]={i}
    #aM = [1]*(N+2)
    #aM = {i:1 for i in range(N+2)}
    aM = np.array([1]*(N+2))

    for _ in range(Q):
        t,d = input().split()
        if t in dS:
            if d == "R":
                for i in dS[t]:
                    if aM[i]:
                        aM[i+1] += aM[i]
                        aM[i] = 0
            else:
                for i in dS[t]:
                    if aM[i]:
                        aM[i-1] += aM[i]
                        aM[i] = 0

    #print(sum(aM[i] for i in range(1,N+1)))
    print(sum(aM[1:N+1]))
解()


Fixed submission:
#こうけ？
def 解():
    import sys
    input = sys.stdin.readline
    N,Q = [int(_) for _ in input().split()]
    S = " " + input() + " "
    aC = [[ _.rstrip() for _ in  _.split()] for _ in sys.stdin.readlines()]
    iL = 0
    iR = N+1
    for t,d in aC[::-1]:
        if t == S[iL+1] and d == "L" and iL < N+1:
                iL += 1
        elif t == S[iL] and d == "R" and 0 < iL:
                iL -= 1

        if t == S[iR-1] and d == "R" and 0 < iR:
                iR -= 1
        elif t == S[iR] and d == "L" and  iR < N+1:
                iR += 1
    if iL < iR:
        print(iR-iL-1)
    else:
        print(0)
解()

Buggy python submission (verdict: Time Limit Exceeded):
#通るかな？
def 解():
    import sys
    input = sys.stdin.readline

    N,Q = [int(_) for _ in input().split()]

    S = input()
    dS = {}
    for i in range(1,N+1):
        s = S[i-1]
        if s in dS:
            dS[s].add(i)
        else:
            dS[s] = {i}
    #aM = [1]*(N+2)
    aM = {i:1 for i in range(N+2)}

    for _ in range(Q):
        t,d = input().split()
        if t in dS:
            if d == "R":
                for i in dS[t]:
                    if aM[i]:
                        aM[i+1] += aM[i]
                        aM[i] = 0
            else:
                for i in dS[t]:
                    if aM[i]:
                        aM[i-1] += aM[i]
                        aM[i] = 0

    print(sum(aM[i] for i in range(1,N+1)))
解()


Fixed submission:
#こうけ？
def 解():
    import sys
    input = sys.stdin.readline
    N,Q = [int(_) for _ in input().split()]
    S = " " + input() + " "
    aC = [[ _.rstrip() for _ in  _.split()] for _ in sys.stdin.readlines()]
    iL = 0
    iR = N+1
    for t,d in aC[::-1]:
        if t == S[iL+1] and d == "L" and iL < N+1:
                iL += 1
        elif t == S[iL] and d == "R" and 0 < iL:
                iL -= 1

        if t == S[iR-1] and d == "R" and 0 < iR:
                iR -= 1
        elif t == S[iR] and d == "L" and  iR < N+1:
                iR += 1
    if iL < iR:
        print(iR-iL-1)
    else:
        print(0)
解()

Buggy python submission (verdict: Time Limit Exceeded):
#通るかな？
def 解():
    import sys
    input = sys.stdin.readline

    N,Q = [int(_) for _ in input().split()]

    S = input()
    dS = {}
    for i in range(1,N+1):
        s = S[i-1]
        if s in dS:
            dS[s].add(i)
        else:
            dS[s] = {i}
    aM = [1]*(N+2)

    for _ in range(Q):
        t,d = input().split()
        if t in dS:
            if d == "R":
                for i in dS[t]:
                    aM[i+1]  += aM[i]
                    aM[i] = 0
            else:
                for i in dS[t]:
                    aM[i-1] += aM[i]
                    aM[i] = 0
    print(sum(aM[1:N+1]))
解()


Fixed submission:
#こうけ？
def 解():
    import sys
    input = sys.stdin.readline
    N,Q = [int(_) for _ in input().split()]
    S = " " + input() + " "
    aC = [[ _.rstrip() for _ in  _.split()] for _ in sys.stdin.readlines()]
    iL = 0
    iR = N+1
    for t,d in aC[::-1]:
        if t == S[iL+1] and d == "L" and iL < N+1:
                iL += 1
        elif t == S[iL] and d == "R" and 0 < iL:
                iL -= 1

        if t == S[iR-1] and d == "R" and 0 < iR:
                iR -= 1
        elif t == S[iR] and d == "L" and  iR < N+1:
                iR += 1
    if iL < iR:
        print(iR-iL-1)
    else:
        print(0)
解()