Problem p03081 (python)

Fixed submission:
N,Q=map(int, input().split())
s=input()
S=len(s)
A=[]
for i in range(Q):
  x,y=input().split()
  A.append((x,y))
  
if N==1:
  for x,y in A:
    if s[0]==x:
      print(0)
      exit()
  print(1)
  exit()
  
def L(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    if s[d]==x and y=='R':
      d+=1
    if d<0:
      return False
    elif d==S:
      d-=1
  return True

ok,ng=S,-1
mid=(ok+ng)//2
while abs(ok-ng)>1:
  mid=(ok+ng)//2
  if L(mid):
    ok=mid
  else:
    ng=mid
p=ok
if ok==S:
  print(0)
  exit()

def R(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    if s[d]==x and y=='R':
      d+=1
    if d>=S:
      return False
    elif d<0:
      d+=1
  return True

ok,ng=-1,S
mid=(ok+ng)//2
while abs(ok-ng)>1:
  mid=(ok+ng)//2
  if R(mid):
    ok=mid
  else:
    ng=mid
q=ok

print(q-p+1)

Buggy submission (verdict: Runtime Error):
N,Q=map(int, input().split())
s=input()
S=len(s)
A=[]
for i in range(Q):
  x,y=input().split()
  A.append((x,y))
  
if N==1:
  for x,y in A:
    if s[0]==x:
      print(0)
      exit()
  print(1)
  exit()
  
def L(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    if s[d]==x and y=='R':
      d+=1
    if d<0:
      return False
  return True

ok,ng=S,-1
mid=(ok+ng)//2
while abs(ok-ng)>1:
  mid=(ok+ng)//2
  if L(mid):
    ok=mid
  else:
    ng=mid
p=ok
if ok==S:
  print(0)
  exit()

def R(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    if s[d]==x and y=='R':
      d+=1
    if d>=S:
      return False
  return True

ok,ng=-1,S
mid=(ok+ng)//2
while abs(ok-ng)>1:
  mid=(ok+ng)//2
  if R(mid):
    ok=mid
  else:
    ng=mid
q=ok

print(q-p+1)

Buggy submission (verdict: Runtime Error):
N,Q=map(int, input().split())
s=input()
S=len(s)
A=[]
for i in range(Q):
  x,y=input().split()
  A.append((x,y))
  
if N==1:
  for x,y in A:
    if s[0]==x:
      print(0)
      exit()
  print(1)
  exit()
  
def L(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    if s[d]==x and y=='R':
      d+=1
    if d<0:
      return False
  return True

ok,ng=S,-1
mid=(ok+ng)//2
for i in range(50):
  mid=(ok+ng)//2
  if L(mid):
    ok=mid
  else:
    ng=mid
p=ok
if ok==S:
  print(0)
  exit()

def R(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    if s[d]==x and y=='R':
      d+=1
    if d>=S:
      return False
  return True

ok,ng=-1,S
mid=(ok+ng)//2
for i in range(50):
  mid=(ok+ng)//2
  if R(mid):
    ok=mid
  else:
    ng=mid
q=ok

print(q-p+1)

Buggy submission (verdict: Runtime Error):
N,Q=map(int, input().split())
s=input()
S=len(s)
A=[]
for i in range(Q):
  x,y=input().split()
  A.append((x,y))
  
if N==1:
  for x,y in A:
    if s[0]==x:
      print(0)
      exit()
  print(1)
  exit()
  
def L(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    elif s[d]==x and y=='R':
      d+=1
    if d<0:
      return False
  return True

ok,ng=S,-1
mid=(ok+ng)//2
for i in range(70):
  mid=(ok+ng)//2
  if L(mid):
    ok=mid
  else:
    ng=mid
p=ok
if ok==S:
  print(0)
  exit()

def R(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    elif s[d]==x and y=='R':
      d+=1
    if d==S:
      return False
  return True

ok,ng=-1,S
mid=(ok+ng)//2
for i in range(70):
  mid=(ok+ng)//2
  if R(mid):
    ok=mid
  else:
    ng=mid
q=ok
print(q-p+1)

Buggy submission (verdict: Runtime Error):
N,Q=map(int, input().split())
s=input()
S=len(s)
A=[]
for i in range(Q):
  x,y=input().split()
  A.append((x,y))
  
if N==1:
  for x,y in A:
    if s[0]==x:
      print(0)
      exit()
  print(1)
  exit()
  
def L(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    elif s[d]==x and y=='R':
      d+=1
    if d<0:
      return False
  return True

ok,ng=S,-1
mid=(ok+ng)//2
for i in range(50):
  mid=(ok+ng)//2
  if L(mid):
    ok=mid
  else:
    ng=mid
p=ok
if ok==S:
  print(0)
  exit()

def R(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    elif s[d]==x and y=='R':
      d+=1
    if d==S:
      return False
  return True

ok,ng=-1,S
mid=(ok+ng)//2
for i in range(50):
  mid=(ok+ng)//2
  if R(mid):
    ok=mid
  else:
    ng=mid
q=ok
print(q-p+1)

Buggy submission (verdict: Runtime Error):
N,Q=map(int, input().split())
s=input()
S=len(s)
A=[]
for i in range(Q):
  x,y=input().split()
  A.append((x,y))

def L(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    elif s[d]==x and y=='R':
      d+=1
    if d<0:
      return False
  return True

ok,ng=S,-1
mid=(ok+ng)//2
for i in range(50):
  mid=(ok+ng)//2
  if L(mid):
    ok=mid
  else:
    ng=mid
p=ok
if ok==S:
  print(0)
  exit()

def R(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    elif s[d]==x and y=='R':
      d+=1
    if d==S:
      return False
  return True

ok,ng=-1,S
mid=(ok+ng)//2
for i in range(50):
  mid=(ok+ng)//2
  if R(mid):
    ok=mid
  else:
    ng=mid
q=ok
print(q-p+1)

Buggy submission (verdict: Runtime Error):
N,Q=map(int, input().split())
s=input()
S=len(s)
A=[]
for i in range(Q):
  x,y=input().split()
  A.append((x,y))

def L(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    elif s[d]==x and y=='R':
      d+=1
    if d<0:
      return False
  return True

ok,ng=S,-1
mid=(ok+ng)//2
for i in range(50):
  mid=(ok+ng)//2
  if L(mid):
    ok=mid
  else:
    ng=mid
p=ok

def R(d):
  for x,y in A:
    if s[d]==x and y=='L':
      d-=1    
    elif s[d]==x and y=='R':
      d+=1
    if d==S:
      return False
  return True

ok,ng=-1,S
mid=(ok+ng)//2
for i in range(50):
  mid=(ok+ng)//2
  if R(mid):
    ok=mid
  else:
    ng=mid
q=ok
print(q-p+1)