Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

        
        
        
        
        
        

Fixed submission:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

        
        
        
        
        
        

Fixed submission:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = len(s)

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = len(s);
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = len(s);
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

        
        
        
        
        
        

Fixed submission:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = len(s)

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = len(s);
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = len(s);
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low
        
        

left_edge = check_left()
#print(left_edge)
right_edge = check_right()
#print(right_edge)

ans_s = s[left_edge+1:right_edge]
#print(ans_s)
ans = len(ans_s)
print(ans)

        
        
        
        
        
        

Fixed submission:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)
    
def is_dead(i):
    LEFT_DIE = -1
    RIGHT_DIE = len(s)
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE or i == RIGHT_DIE:
                return True
    return False

def check_left():
    tmp = -1
    for i in range(len(s)):
        if is_dead(i):
            tmp = i
        else:
            return tmp

def check_right():
    tmp = len(s)
    for i in range(len(s)):
        i = len(s) - i - 1
        if is_dead(i):
            tmp = i
        else:
            return tmp
        
        

left_edge = check_left()
#print(left_edge)
right_edge = check_right()
#print(right_edge)

ans_s = s[left_edge+1:right_edge]
#print(ans_s)
ans = len(ans_s)
print(ans)

        
        
        
        
        
        

Fixed submission:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)
    
spell_target = {}
for i, c in enumerate(s):
    if c in spell_target:
        spell_target[c].append(i+1)
    else:
        spell_target[c] = [i+1]
#print(spell_target)

golems_nums = [1 for _ in range(len(s) + 2)]
golems_nums[0] = 0
golems_nums[-1] = 0
#print(golems_nums)

for ti, di in zip(t, d):
    if not ti in spell_target:
        continue
    if di == 'L':
        targets = spell_target[ti]
        for n in targets:
            golems_nums[n-1] += golems_nums[n]
            golems_nums[n] = 0
    if di == 'R':
        targets = spell_target[ti]
        for n in reversed(targets):
            golems_nums[n+1] += golems_nums[n]
            golems_nums[n] = 0
#print(golems_nums)
ans = sum(golems_nums) - golems_nums[0] - golems_nums[-1]
print(ans)



Fixed submission:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)
    
spell_target = {}
for i, c in enumerate(s):
    if c in spell_target:
        spell_target[c].append(i+1)
    else:
        spell_target[c] = [i+1]
#print(spell_target)

golems_nums = [1 for _ in range(len(s) + 2)]
golems_nums[0] = 0
golems_nums[-1] = 0
#print(golems_nums)

for ti, di in zip(t, d):
    if di == 'L':
        targets = spell_target[ti]
        for n in targets:
            golems_nums[n-1] += golems_nums[n]
            golems_nums[n] = 0
    if di == 'R':
        targets = spell_target[ti]
        for n in reversed(targets):
            golems_nums[n+1] += golems_nums[n]
            golems_nums[n] = 0
#print(golems_nums)
ans = sum(golems_nums) - golems_nums[0] - golems_nums[-1]
print(ans)

Fixed submission:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)
    
class Golem:
    def __init__(self, number, text):
        self.number = number
        self.text = text
        #self.life = True
        
    def move(self, dist):
        if dist == 'L':
            if self.number == 0:
                return 'DEL'
            else:
                self.number -= 1
                self.text = s[self.number]
                return self.text
        if dist == 'R':
            if self.number == len(s)-1:
                return 'DEL'
            else:
                self.number += 1
                self.text = s[self.number]
                return self.text
                

c_dict = {}
for i, c in enumerate(s):
    if c in c_dict:
        c_dict[c].append(Golem(i, c))
    else:
        c_dict[c] = [Golem(i, c)]
        
for ti, di in zip(t, d):
    if not ti in c_dict:
        continue
    golems = c_dict[ti][:]
    c_dict[ti] = []
    for g in golems:
        res = g.move(di)
        if res != 'DEL':
            c_dict[res].append(g)

#print(c_dict)
ans = 0
for k, v in c_dict.items():
    ans += len(v)
print(ans)

    

Fixed submission:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)


def main_fanc(s, t, d, N):
    masu = {}
    for i, si in enumerate(s):
        if not si in masu:
            masu[si] = [i]
        else:
            masu[si].append(i)

    
    g = [1 for _ in range(N)]
    
    for ti, di in zip(t, d):
        if not ti in masu:
            continue
        if di == 'L':
            for pos in masu[ti]:
                if pos == 0:
                    g[pos] = 0
                else:
                    g[pos - 1] += g[pos]
                    g[pos] = 0
        if di == 'R':

            for pos in reversed(masu[ti]):
                if pos == N - 1:
                    g[pos] = 0
                else:
                    g[pos + 1] += g[pos]
                    g[pos] = 0
    
    print(sum(g))
                    

main_fanc(s, t, d, N)

Fixed submission:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)


def main_fanc(s, t, d, N):
    masu = []
    for si in s:
        masu.append({'key': si, 'val': 1})
        
    for ti, di in zip(t, d):
        if di == 'L':
            for i, m in enumerate(masu):
                if m['val'] == 0:
                    continue
                if m['key'] != ti:
                    continue
                if i == 0:
                    masu[i]['val'] = 0
                else:
                    masu[i - 1]['val'] += masu[i]['val']
                    masu[i]['val'] = 0
                    
        if di == 'R':

            for i, m in enumerate(masu):
                if m['val'] == 0:
                    continue
                if m['key'] != ti:
                    continue
                if i == N - 1:
                    masu[i]['val'] = 0
                else:
                    masu[i + 1]['val'] += masu[i]['val']
                    masu[i]['val'] = 0
    
    c = 0
    for m in masu:
        c += m['val']
    
    print(c)
                
main_fanc(s, t, d, N)

Fixed submission:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)


def main_fanc(s, t, d, N):
    masu = []
    for si in s:
        masu.append({'key': si, 'val': 1})
        
    for ti, di in zip(t, d):
        if di == 'L':
            for i, m in enumerate(masu):
                if m['key'] != ti:
                    continue
                if i == 0:
                    masu[i]['val'] = 0
                else:
                    masu[i - 1]['val'] += masu[i]['val']
                    masu[i]['val'] = 0
                    
        if di == 'R':

            for i, m in enumerate(masu):
                if m['key'] != ti:
                    continue
                if i == N - 1:
                    masu[i]['val'] = 0
                else:
                    masu[i + 1]['val'] += masu[i]['val']
                    masu[i]['val'] = 0
    
    c = 0
    for m in masu:
        c += m['val']
    
    print(c)
                
main_fanc(s, t, d, N)

Fixed submission:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)


def main_fanc(s, t, d, N):
    masu = []
    for si in s:
        masu.append({'key': si, 'val': 1})
        
    for ti, di in zip(t, d):
        if di == 'L':
            for i, m in enumerate(masu):
                if m['key'] != ti:
                    continue
                if i == 0:
                    masu[i]['val'] = 0
                else:
                    masu[i - 1]['val'] += masu[i]['val']
                    masu[i]['val'] = 0
                    
        if di == 'R':
            masu.reverse()
            for i, m in enumerate(masu):
                if m['key'] != ti:
                    continue
                if i == 0:
                    masu[i]['val'] = 0
                else:
                    masu[i - 1]['val'] += masu[i]['val']
                    masu[i]['val'] = 0
            masu.reverse()
    
    c = 0
    for m in masu:
        c += m['val']
    
    print(c)
                
main_fanc(s, t, d, N)

Fixed submission:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)