Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
s = input()
td = [input().split() for i in range(Q)]
#print("td = {}".format(td))

llim = 0
rlim = N - 1
for t, d in reversed(td):
    if("L" == d):
        if(s[llim] == t):
            llim += 1
        if(rlim < N - 1):
            if(s[rlim + 1] == t):
                rlim += 1
    else:# if("R" == d):
        if(s[rlim] == t):
            rlim -= 1
        if(llim > 0):
            if(s[llim - 1] == t):
                llim -= 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, rlim - llim + 1)))


Fixed submission:
N, Q = map(int, input().split())
s = " " + input() + " "
td = []
for i in range(Q):
    td.append(input().split())
#print("td = {}".format(td))

llim = 0
rlim = N + 1
for t, d in reversed(td):
    if((s[llim + 1] == t) and ("L" == d)):
        llim += 1
    elif((s[llim] == t) and ("R" == d)):
        llim -= 1
    if((s[rlim - 1] == t) and ("R" == d)):
        rlim -= 1
    elif((s[rlim] == t) and ("L" == d)):
        rlim += 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, rlim - llim - 1)))

Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
s = input()
td = []
for i in range(Q):
    td.append(input().split())
#print("td = {}".format(td))

llim = 0
rlim = - 1
for t, d in reversed(td):
    if((s[llim] == t) and ("L" == d)):
        llim += 1
    elif(llim > 0):
        if((s[llim - 1] == t) and ("R" == d)):
            llim -= 1
    if((s[rlim] == t) and ("R" == d)):
        rlim -= 1
    elif(rlim < - 1):
        if((s[rlim + 1] == t) and ("L" == d)):
            rlim += 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, N + rlim - llim + 1)))


Fixed submission:
N, Q = map(int, input().split())
s = " " + input() + " "
td = []
for i in range(Q):
    td.append(input().split())
#print("td = {}".format(td))

llim = 0
rlim = N + 1
for t, d in reversed(td):
    if((s[llim + 1] == t) and ("L" == d)):
        llim += 1
    elif((s[llim] == t) and ("R" == d)):
        llim -= 1
    if((s[rlim - 1] == t) and ("R" == d)):
        rlim -= 1
    elif((s[rlim] == t) and ("L" == d)):
        rlim += 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, rlim - llim - 1)))

Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
s = input()
td = []
for i in range(Q):
    td.append(input().split())
#print("td = {}".format(td))

llim = 0
rlim = N - 1
for t, d in reversed(td):
    if((s[llim] == t) and ("L" == d)):
        llim += 1
    elif(llim - 1 >= 0):
        if((s[llim - 1] == t) and ("R" == d)):
            llim -= 1
    if((s[rlim] == t) and ("R" == d)):
        rlim -= 1
    elif(rlim + 1 <= N - 1):
        if((s[rlim + 1] == t) and ("L" == d)):
            rlim += 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, rlim - llim + 1)))


Fixed submission:
N, Q = map(int, input().split())
s = " " + input() + " "
td = []
for i in range(Q):
    td.append(input().split())
#print("td = {}".format(td))

llim = 0
rlim = N + 1
for t, d in reversed(td):
    if((s[llim + 1] == t) and ("L" == d)):
        llim += 1
    elif((s[llim] == t) and ("R" == d)):
        llim -= 1
    if((s[rlim - 1] == t) and ("R" == d)):
        rlim -= 1
    elif((s[rlim] == t) and ("L" == d)):
        rlim += 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, rlim - llim - 1)))

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = " " + input() + " "
td = []
for i in range(Q):
    td.insert(0, input().split())
#print("td = {}".format(td))

llim = 0
rlim = N + 1
for t, d in td:
    if((s[llim + 1] == t) and ("L" == d)):
        llim += 1
    elif((s[llim] == t) and ("R" == d)):
        llim -= 1
    if((s[rlim - 1] == t) and ("R" == d)):
        rlim -= 1
    elif((s[rlim] == t) and ("L" == d)):
        rlim += 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, rlim - llim - 1)))


Fixed submission:
N, Q = map(int, input().split())
s = " " + input() + " "
td = []
for i in range(Q):
    td.append(input().split())
#print("td = {}".format(td))

llim = 0
rlim = N + 1
for t, d in reversed(td):
    if((s[llim + 1] == t) and ("L" == d)):
        llim += 1
    elif((s[llim] == t) and ("R" == d)):
        llim -= 1
    if((s[rlim - 1] == t) and ("R" == d)):
        rlim -= 1
    elif((s[rlim] == t) and ("L" == d)):
        rlim += 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, rlim - llim - 1)))

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = input()
td = []
for i in range(Q):
    td.insert(0, input().split())
#print("td = {}".format(td))

llim = 0
rlim = N - 1
for t, d in td:
    if((s[llim] == t) and ("L" == d)):
        llim += 1
    elif(llim - 1 >= 0):
        if((s[llim - 1] == t) and ("R" == d)):
            llim -= 1
    if((s[rlim] == t) and ("R" == d)):
        rlim -= 1
    elif(rlim + 1 <= N - 1):
        if((s[rlim + 1] == t) and ("L" == d)):
            rlim += 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, rlim - llim + 1)))


Fixed submission:
N, Q = map(int, input().split())
s = " " + input() + " "
td = []
for i in range(Q):
    td.append(input().split())
#print("td = {}".format(td))

llim = 0
rlim = N + 1
for t, d in reversed(td):
    if((s[llim + 1] == t) and ("L" == d)):
        llim += 1
    elif((s[llim] == t) and ("R" == d)):
        llim -= 1
    if((s[rlim - 1] == t) and ("R" == d)):
        rlim -= 1
    elif((s[rlim] == t) and ("L" == d)):
        rlim += 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, rlim - llim - 1)))

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = input()
td = []
for i in range(Q):
    td.insert(0, input().split())

#print("td = {}".format(td))

llim = 0
rlim = N - 1
for t, d in td:
    if((s[llim] == t) & ("L" == d)):
        llim += 1
    if((s[rlim] == t) & ("R" == d)):
        rlim -= 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

if(llim > rlim):
    print("0")
else:
    print(str(rlim - llim + 1))


Fixed submission:
N, Q = map(int, input().split())
s = " " + input() + " "
td = []
for i in range(Q):
    td.append(input().split())
#print("td = {}".format(td))

llim = 0
rlim = N + 1
for t, d in reversed(td):
    if((s[llim + 1] == t) and ("L" == d)):
        llim += 1
    elif((s[llim] == t) and ("R" == d)):
        llim -= 1
    if((s[rlim - 1] == t) and ("R" == d)):
        rlim -= 1
    elif((s[rlim] == t) and ("L" == d)):
        rlim += 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, rlim - llim - 1)))

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = input()
T = []
D = []
for i in range(Q):
    t, d = input().split()
    T.append(t)
    D.append(d)

s_idx = {}
s_inv = {}
for i in range(N):
    if(s[i] in s_idx):
        s_idx[s[i]] = s_idx[s[i]] + [i]
        s_inv[s[i]] = [i] + s_idx[s[i]]
    else:
        s_idx[s[i]] = [i]
        s_inv[s[i]] = [i]
#print("s_idx={}".format(s_idx))

golems = [1] * N
g_num = N
for idx in range(Q):
#    print("spl={}, {}".format([T[idx], D[idx]], T[idx] in s_idx))
    if(True != (T[idx] in s_idx)):
        continue
    if("L" == D[idx]):
        for i in s_idx[T[idx]]:
            if(0 == i):
                g_num -= golems[i]
            else:
                golems[i - 1] += golems[i]
            golems[i] = 0
    elif("R" == D[idx]):
        for i in s_inv[T[idx]]:
            if(N - 1 == i):
                g_num -= golems[i]
            else:
                golems[i + 1] += golems[i]
            golems[i] = 0

print(str(g_num))


Fixed submission:
N, Q = map(int, input().split())
s = " " + input() + " "
td = []
for i in range(Q):
    td.append(input().split())
#print("td = {}".format(td))

llim = 0
rlim = N + 1
for t, d in reversed(td):
    if((s[llim + 1] == t) and ("L" == d)):
        llim += 1
    elif((s[llim] == t) and ("R" == d)):
        llim -= 1
    if((s[rlim - 1] == t) and ("R" == d)):
        rlim -= 1
    elif((s[rlim] == t) and ("L" == d)):
        rlim += 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, rlim - llim - 1)))

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = input()
spells = []
for i in range(Q):
    spells.append(list(input().split()))
T = 0
D = 1

s_idx = {}
for i in range(N):
    if(s[i] in s_idx):
        s_idx[s[i]] += [i]
    else:
        s_idx[s[i]] = [i]
#print("s_idx={}".format(s_idx))

golems = [1] * N
g_num = N
for spl in spells:
#    print("spl={}, {}".format(spl, spl[T] in s_idx))
    if(True != (spl[T] in s_idx)):
#        print("skip")
        continue
    if("L" == spl[D]):
        for i in sorted(s_idx[spl[T]]):
            if(0 == i):
                g_num -= golems[i]
            else:
                golems[i - 1] += golems[i]
            golems[i] = 0
    elif("R" == spl[D]):
        for i in sorted(s_idx[spl[T]], reverse=True):
            if(N - 1 == i):
                g_num -= golems[i]
            else:
                golems[i + 1] += golems[i]
            golems[i] = 0

print(str(g_num))


Fixed submission:
N, Q = map(int, input().split())
s = " " + input() + " "
td = []
for i in range(Q):
    td.append(input().split())
#print("td = {}".format(td))

llim = 0
rlim = N + 1
for t, d in reversed(td):
    if((s[llim + 1] == t) and ("L" == d)):
        llim += 1
    elif((s[llim] == t) and ("R" == d)):
        llim -= 1
    if((s[rlim - 1] == t) and ("R" == d)):
        rlim -= 1
    elif((s[rlim] == t) and ("L" == d)):
        rlim += 1
#    print("td = {}, llim = {}, rlim = {}".format([t, d], llim, rlim))

print(str(max(0, rlim - llim - 1)))