Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python3
N, Q = [int(x) for x in input().split()]
S = input()
#print(S)

qs = []
for _ in range(Q):
    t, d = [x for x in input().split()]
    #print(t, d)
    qs.append((t,-1 if d == 'L' else 1))

memo = [None] * N

def move(i):
    org = i
    if memo[i]:
        return memo[i]
    for t, d in qs:
        if S[i] == t:
            i+=d
        if i == -1:
            break
        elif i == N:
            break
    memo[org] = i
    return i

def search_right(lo, hi, t):
    while lo < hi:
        m = lo + (hi - lo) // 2
        e = move(m)
        msg = "l=%d, r=%d, m=%d\t=> last[%d] = %3d (t=%d)" % (lo, hi, m, m, e, t)
        # 
        if e <= t:
            lo = m + 1
        else: # e <= t
            hi = m
    return lo

def search_left(lo, hi, t):
    while lo < hi:
        m = lo + (hi - lo) // 2
        e = move(m)
        msg = "l=%d, r=%d, m=%d\t=> last[%d] = %3d (t=%d)" % (lo, hi, m, m, e, t)
        # 
        if t < e:
            hi = m
        else: # e <= t
            lo = m +1
    return lo


#l = search(0, N-1, -1)
#assert move(l) == 2, "%d" % move(l)
#print("search(-1)=", l)

l = search_right(0, N-1, -1)
r = search_left(0, N, N-1)

m = [0] * (N)
for i in range(N):
    m[i] = move(i)

#print(" ".join("%2d" % i for i in range(N+1)))
#print(" ".join("%+d" % x for x in m))
#print(" ".join(" *" if l == i else "  " for i in range(N+1)))
#print(" ".join(" *" if r == i else "  " for i in range(N+1)))

print(r-l)





Fixed submission:
#!/usr/bin/python3
N, Q = [int(x) for x in input().split()]
S = input()

qs = []
for _ in range(Q):
    t, d = [x for x in input().split()]
    #print(t, d)
    qs.append((t,-1 if d == 'L' else 1))

memo = [None] * N
memo.append(N)

def move(i):
    org = i
    if memo[i]:
        return memo[i]
    for t, d in qs:
        if S[i] == t:
            i+=d
        if i == -1:
            break
        elif i == N:
            break
    memo[org] = i
    return i

def search_right(lo, hi, x):
    while lo < hi:
        mid = lo + (hi - lo) // 2
        if x < move(mid):
            hi = mid 
        else: # e <= t
            lo = mid +1
    return lo

def search_left(lo, hi, x):
    while lo < hi:
        mid = lo + (hi - lo) // 2
        if move(mid) < x:
            lo = mid +1
        else: # e <= t
            hi = mid
    return lo


#l = search(0, N-1, -1)
#assert move(l) == 2, "%d" % move(l)
#print("search(-1)=", l)

l = search_right(0, N, -1)
r = search_left(0, N, N)

if 1==0:
    print(S)
    for t, d in qs:
        print(t, {-1:'L', 1:'R'}[d])
    m = [move(i) for i in range(N+1)]
    print(" ".join("%2d" % i for i in range(N+1)))
    print(" ".join("%+d" % x for x in m))
    print(" ".join(" *" if l == i else "  " for i in range(N+1)))
    print(" ".join(" *" if r == i else "  " for i in range(N+1)))

print(r-l)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = [int(x) for x in input().split()]
G = [1] * N

index={}
for i,s in enumerate(input().strip()):
    index.setdefault(s, []).append(i)
import sys
input = sys.stdin.readline

for _ in range(Q):
    t, _, d = input()[:3]
    if t not in index:
        continue

    if d == 'L':
        for i in index[t]:
            if i > 0:
                G[i-1]+= G[i]
            G[i] = 0

    else:
        for i in index[t]:
            if i < N - 1:
                G[i+1] += G[i]
            G[i] = 0
print(sum(G)) 


Fixed submission:
#!/usr/bin/python3
N, Q = [int(x) for x in input().split()]
S = input()

qs = []
for _ in range(Q):
    t, d = [x for x in input().split()]
    #print(t, d)
    qs.append((t,-1 if d == 'L' else 1))

memo = [None] * N
memo.append(N)

def move(i):
    org = i
    if memo[i]:
        return memo[i]
    for t, d in qs:
        if S[i] == t:
            i+=d
        if i == -1:
            break
        elif i == N:
            break
    memo[org] = i
    return i

def search_right(lo, hi, x):
    while lo < hi:
        mid = lo + (hi - lo) // 2
        if x < move(mid):
            hi = mid 
        else: # e <= t
            lo = mid +1
    return lo

def search_left(lo, hi, x):
    while lo < hi:
        mid = lo + (hi - lo) // 2
        if move(mid) < x:
            lo = mid +1
        else: # e <= t
            hi = mid
    return lo


#l = search(0, N-1, -1)
#assert move(l) == 2, "%d" % move(l)
#print("search(-1)=", l)

l = search_right(0, N, -1)
r = search_left(0, N, N)

if 1==0:
    print(S)
    for t, d in qs:
        print(t, {-1:'L', 1:'R'}[d])
    m = [move(i) for i in range(N+1)]
    print(" ".join("%2d" % i for i in range(N+1)))
    print(" ".join("%+d" % x for x in m))
    print(" ".join(" *" if l == i else "  " for i in range(N+1)))
    print(" ".join(" *" if r == i else "  " for i in range(N+1)))

print(r-l)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = [int(x) for x in input().split()]
G = [1] * N

index={}
for i,s in enumerate(input().strip()):
    index.setdefault(s, []).append(i)

for _ in range(Q):
    t, d = [x for x in input().split()]
    if t not in index:
        continue

    if d == 'L':
        for i in index[t]:
            if i > 0:
                G[i-1]+= G[i]
            G[i] = 0

    else:
        for i in index[t]:
            if i < N - 1:
                G[i+1] += G[i]
            G[i] = 0
print(sum(G)) 


Fixed submission:
#!/usr/bin/python3
N, Q = [int(x) for x in input().split()]
S = input()

qs = []
for _ in range(Q):
    t, d = [x for x in input().split()]
    #print(t, d)
    qs.append((t,-1 if d == 'L' else 1))

memo = [None] * N
memo.append(N)

def move(i):
    org = i
    if memo[i]:
        return memo[i]
    for t, d in qs:
        if S[i] == t:
            i+=d
        if i == -1:
            break
        elif i == N:
            break
    memo[org] = i
    return i

def search_right(lo, hi, x):
    while lo < hi:
        mid = lo + (hi - lo) // 2
        if x < move(mid):
            hi = mid 
        else: # e <= t
            lo = mid +1
    return lo

def search_left(lo, hi, x):
    while lo < hi:
        mid = lo + (hi - lo) // 2
        if move(mid) < x:
            lo = mid +1
        else: # e <= t
            hi = mid
    return lo


#l = search(0, N-1, -1)
#assert move(l) == 2, "%d" % move(l)
#print("search(-1)=", l)

l = search_right(0, N, -1)
r = search_left(0, N, N)

if 1==0:
    print(S)
    for t, d in qs:
        print(t, {-1:'L', 1:'R'}[d])
    m = [move(i) for i in range(N+1)]
    print(" ".join("%2d" % i for i in range(N+1)))
    print(" ".join("%+d" % x for x in m))
    print(" ".join(" *" if l == i else "  " for i in range(N+1)))
    print(" ".join(" *" if r == i else "  " for i in range(N+1)))

print(r-l)