Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
S = input()
TD = reversed([tuple((input().split())) for _ in range(Q)])

left, right = 0, N-1

for t, d in TD:
	if S[left] == t and "L" == d:
		left += 1
	if S[right] == t and "R" == d:
		right -= 1
print(right-left+1)


Fixed submission:
N, Q = map(int, input().split())
S = list(input())
TD = [tuple(input().split()) for _ in range(Q)]

def bs_l(f, s, e):
	if s==e:
		return s if f(s) else s+1

	c = (s+e)//2
	if f(c):
		#左
		return bs_l(f, s, c if c == s else c-1)
	else:
		#右
		return bs_l(f, c if c == e else c+1, e)

def bs_r(f, s, e):
	if s==e:
		return s if f(s) else s-1

	c = (s+e)//2
	if not f(c):
		#左
		return bs_r(f, s, c if c == s else c-1)
	else:
		#右
		return bs_r(f, c if c == e else c+1, e)

def isAlive(c):
	for t, d in TD:
		if S[c] == t:
			c += 1 if "R" == d else -1
			if c < 0 or N <= c:
				return False
	return True

S, E = bs_l(isAlive, 0, N-1), bs_r(isAlive, 0, N-1)
print(max(E-S+1, 0))

Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
S = list(input())
TD = [tuple(input().split()) for _ in range(Q)]

def bs_l(f, s, e):
	if s==e:
		return s if(1==f(s)) else s+1

	c = (s+e)//2
	if f(c):
		#左
		return bs_l(f, s, c if c == s else c-1)
	else:
		#右
		return bs_l(f, c if c == e else c+1, e)

def bs_r(f, s, e):
	if s==e:
		return s if(1==f(s)) else s-1

	c = (s+e)//2
	if not f(c):
		#左
		return bs_r(f, s, c if c == s else c-1)
	else:
		#右
		return bs_r(f, c if c == e else c+1, e)

def isAlive_l(c):
	if c < 0 or N <= c:
		return False

	for t, d in TD:
		if S[c] == t:
			c += 1 if "R" == d else -1
			if c < 0:
				return False
	return True

def isAlive_r(c):
	if c < 0 or N <= c:
		return False

	for t, d in TD:
		if S[c] == t:
			c += 1 if "R" == d else -1
			if N <= c:
				return False
	return True

S, E = bs_l(isAlive_l, 0, N-1), bs_r(isAlive_r, 0, N-1)
print(E-S+1)


Fixed submission:
N, Q = map(int, input().split())
S = list(input())
TD = [tuple(input().split()) for _ in range(Q)]

def bs_l(f, s, e):
	if s==e:
		return s if f(s) else s+1

	c = (s+e)//2
	if f(c):
		#左
		return bs_l(f, s, c if c == s else c-1)
	else:
		#右
		return bs_l(f, c if c == e else c+1, e)

def bs_r(f, s, e):
	if s==e:
		return s if f(s) else s-1

	c = (s+e)//2
	if not f(c):
		#左
		return bs_r(f, s, c if c == s else c-1)
	else:
		#右
		return bs_r(f, c if c == e else c+1, e)

def isAlive(c):
	for t, d in TD:
		if S[c] == t:
			c += 1 if "R" == d else -1
			if c < 0 or N <= c:
				return False
	return True

S, E = bs_l(isAlive, 0, N-1), bs_r(isAlive, 0, N-1)
print(max(E-S+1, 0))

Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
S = list(input())
TD = [tuple(input().split()) for _ in range(Q)]

TEST = [0, 0, 0, 1, 1, 1, 0, 0]

def bs_l(f, s, e):
	if s==e:
		return s if(1==f(s)) else s+1

	c = (s+e)//2
	if f(c):
		#左
		return bs_l(f, s, c if c == s else c-1)
	else:
		#右
		return bs_l(f, c if c == e else c+1, e)

def bs_r(f, s, e):
	if s==e:
		return s if(1==f(s)) else s-1

	c = (s+e)//2
	if not f(c):
		#左
		return bs_r(f, s, c if c == s else c-1)
	else:
		#右
		return bs_r(f, c if c == e else c+1, e)

def isAlive_l(c):
	for t, d in TD:
		if S[c] == t:
			c += 1 if "R" == d else -1
			if c < 0:
				return False
	return True

def isAlive_r(c):
	for t, d in TD:
		if S[c] == t:
			c += 1 if "R" == d else -1
			if N <= c:
				return False
	return True

S, E = bs_l(isAlive_l, 0, N-1), bs_r(isAlive_r, 0, N-1)
print(E-S+1)


Fixed submission:
N, Q = map(int, input().split())
S = list(input())
TD = [tuple(input().split()) for _ in range(Q)]

def bs_l(f, s, e):
	if s==e:
		return s if f(s) else s+1

	c = (s+e)//2
	if f(c):
		#左
		return bs_l(f, s, c if c == s else c-1)
	else:
		#右
		return bs_l(f, c if c == e else c+1, e)

def bs_r(f, s, e):
	if s==e:
		return s if f(s) else s-1

	c = (s+e)//2
	if not f(c):
		#左
		return bs_r(f, s, c if c == s else c-1)
	else:
		#右
		return bs_r(f, c if c == e else c+1, e)

def isAlive(c):
	for t, d in TD:
		if S[c] == t:
			c += 1 if "R" == d else -1
			if c < 0 or N <= c:
				return False
	return True

S, E = bs_l(isAlive, 0, N-1), bs_r(isAlive, 0, N-1)
print(max(E-S+1, 0))

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
S = list(input())
TD = [tuple(input().split()) for _ in range(Q)]
G = [1 for _ in range(N)]

TD_ALPHA = dict()
for i, s in enumerate(S):
	if s in TD_ALPHA:
		TD_ALPHA[s] += [i]
	else:
		TD_ALPHA[s] = [i]

for td in TD:
	if td[0] in TD_ALPHA:
		for i in TD_ALPHA[td[0]]:
			if "L" == td[1]:
				if 0 == i:
					G[i] = 0
				else:
					G[i-1] += G[i]
					G[i] = 0
			else:
				if N-1 == i:
					G[i] = 0
				else:
					G[i+1] += G[i]
					G[i] = 0
print(sum(G))


Fixed submission:
N, Q = map(int, input().split())
S = list(input())
TD = [tuple(input().split()) for _ in range(Q)]

def bs_l(f, s, e):
	if s==e:
		return s if f(s) else s+1

	c = (s+e)//2
	if f(c):
		#左
		return bs_l(f, s, c if c == s else c-1)
	else:
		#右
		return bs_l(f, c if c == e else c+1, e)

def bs_r(f, s, e):
	if s==e:
		return s if f(s) else s-1

	c = (s+e)//2
	if not f(c):
		#左
		return bs_r(f, s, c if c == s else c-1)
	else:
		#右
		return bs_r(f, c if c == e else c+1, e)

def isAlive(c):
	for t, d in TD:
		if S[c] == t:
			c += 1 if "R" == d else -1
			if c < 0 or N <= c:
				return False
	return True

S, E = bs_l(isAlive, 0, N-1), bs_r(isAlive, 0, N-1)
print(max(E-S+1, 0))

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
S = list(input())
TD = [tuple(input().split()) for _ in range(Q)]
G = [1 for _ in range(N)]

for td in TD:
	for i, s in enumerate(S):
		if s == td[0]:
			if "L" == td[1]:
				if 0 == i:
					G[i] = 0
				else:
					G[i-1] += G[i]
					G[i] = 0
			else:
				if N-1 == i:
					G[i] = 0
				else:
					G[i+1] += G[i]
					G[i] = 0
print(sum(G))


Fixed submission:
N, Q = map(int, input().split())
S = list(input())
TD = [tuple(input().split()) for _ in range(Q)]

def bs_l(f, s, e):
	if s==e:
		return s if f(s) else s+1

	c = (s+e)//2
	if f(c):
		#左
		return bs_l(f, s, c if c == s else c-1)
	else:
		#右
		return bs_l(f, c if c == e else c+1, e)

def bs_r(f, s, e):
	if s==e:
		return s if f(s) else s-1

	c = (s+e)//2
	if not f(c):
		#左
		return bs_r(f, s, c if c == s else c-1)
	else:
		#右
		return bs_r(f, c if c == e else c+1, e)

def isAlive(c):
	for t, d in TD:
		if S[c] == t:
			c += 1 if "R" == d else -1
			if c < 0 or N <= c:
				return False
	return True

S, E = bs_l(isAlive, 0, N-1), bs_r(isAlive, 0, N-1)
print(max(E-S+1, 0))