Problem p03488 (python)

Fixed submission:
S = input()
X,Y = map(int,input().split())

# [文字,その文字が連続する数]
from collections import deque
sumS = deque()
import itertools
for key,g in itertools.groupby(S):
  sumS.append([key,len(list(g))])
  
startx = 0 # Fで始まる場合はX軸の正の方向にあらかじめ進めて置く
# 最初のTが出るまではX軸正の方向に進む
if sumS[0][0] == "F":
  startx = sumS[0][1]
  sumS.popleft()

xdir = True # X軸方向を向いている
dpXcur = set()
dpXcur.add(startx)
dpYcur = set()
dpYcur.add(0) # Y軸は必ず0から始まる
dpXnex = set()
dpYnex = set()
for i in range(len(sumS)):
  if sumS[i][0] == "F":
    steps = sumS[i][1] # 進む数
    if xdir: # X軸方向を向いている場合
      for x in dpXcur:
        dpXnex.add(x + steps)
        dpXnex.add(x - steps)
      dpXcur = dpXnex
      dpXnex = set()
    else:
      for y in dpYcur:
        dpYnex.add(y + steps)
        dpYnex.add(y - steps)
      dpYcur = dpYnex
      dpYnex = set()
  elif sumS[i][0] == "T":
    turns = sumS[i][1] # 方向転換する数
    if turns % 2 == 1:
      # 奇数回の場合はX/Y軸の切り替え (偶数回の場合は何も起こらない)
      xdir ^= True
      
if X in dpXcur and Y in dpYcur:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
S=input()
x,y=map(int,input().split())
import itertools
Xmove=[]
Ymove=[]
Xbase=0
Ybase=0
Xtar=True
for key,g in itertools.groupby(S):
  num=len(list(g))
  if key=="T":
    if num%2==1:
      Xtar^=True
  else:
    if Xtar:
      Xmove.append(num)
      Xbase+=num
    else:
      Ymove.append(num)
      Ybase+=num
if Xbase<x or Ybase<y:
  print("No")
  exit(0)

dpX=[[False for i in range(Xbase*2+1)] for j in range(2)]
dpY=[[False for i in range(Ybase*2+1)] for j in range(2)]
Xind=0

if S[0]=="F":
  dpX[0][Xbase+Xmove[0]]=True
  Xind=1
else:
  dpX[0][Xbase]=True
dpY[0][Ybase]=True

cur=0
for i in range(Xind,len(Xmove)):
  for j in range(len(dpX[cur])):
    if dpX[cur][j]:
      dpX[1-cur][j+Xmove[i]]=True
      dpX[1-cur][j-Xmove[i]]=True
      dpX[cur][j]=False
  cur=1-cur
  
if not dpX[cur][x+Xbase]:
  print("No")
  exit(0)
  
cur=0
for i in range(len(Ymove)):
  for j in range(len(dpY[cur])):
    if dpY[cur][j]:
      dpY[1-cur][j+Ymove[i]]=True
      dpY[1-cur][j-Ymove[i]]=True
      dpY[cur][j]=False
  cur=1-cur
  
if dpY[cur][y+Ybase]:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
S=input()
x,y=map(int,input().split())
import itertools
Xmove=[]
Ymove=[]
Xbase=0
Ybase=0
Xtar=True
for key,g in itertools.groupby(S):
  num=len(list(g))
  if key=="T":
    if num%2==1:
      Xtar^=True
  else:
    if Xtar:
      Xmove.append(num)
      Xbase+=num
    else:
      Ymove.append(num)
      Ybase+=num
if Xbase<x or Ybase<y:
  print("No")
  exit(0)

dpX=[[False for i in range(Xbase*2+1)] for j in range(2)]
dpY=[[False for i in range(Ybase*2+1)] for j in range(2)]
Xind=0

if S[0]=="F":
  dpX[0][Xbase+Xmove[0]]=True
  Xind=1
else:
  dpX[0][Xbase]=True
dpY[0][Ybase]=True

cur=0
for i in range(Xind,len(Xmove)):
  for j in range(len(dpX[cur])):
    if dpX[cur][j]:
      dpX[1-cur][j+Xmove[i]]=True
      dpX[1-cur][j-Xmove[i]]=True
      dpX[cur][j]=False
  cur=1-cur
  
if not dpX[cur][x+Xbase]:
  print("No")
  exit(0)
  
cur=0
for i in range(len(Ymove)):
  for j in range(len(dpY[cur])):
    if dpY[cur][j]:
      dpY[1-cur][j+Ymove[i]]=True
      dpY[1-cur][j-Ymove[i]]=True
      dpY[cur][j]=False
  cur=1-cur
  
if dpY[cur][y+Ybase]:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
S=input()
x,y=map(int,input().split())
import itertools
Xmove=[]
Ymove=[]
Xbase=0
Ybase=0
Xtar=True
for key,g in itertools.groupby(S):
  num=len(list(g))
  if key=="T":
    if num%2==1:
      Xtar^=True
  else:
    if Xtar:
      Xmove.append(num)
      Xbase+=num
    else:
      Ymove.append(num)
      Ybase+=num
if Xbase<x or Ybase<y:
  print("No")
  exit(0)

dpX=[[False for i in range(Xbase*2+1)] for j in range(2)]
dpY=[[False for i in range(Ybase*2+1)] for j in range(2)]
Xind=0

if S[0]=="F":
  dpX[0][Xbase+Xmove[0]]=True
  Xind=1
else:
  dpx[0][Xbase]=True
dpY[0][Ybase]=True

cur=0
for i in range(Xind,len(Xmove)):
  for j in range(len(dpX[cur])):
    if dpX[cur][j]:
      dpX[1-cur][j+Xmove[i]]=True
      dpX[1-cur][j-Xmove[i]]=True
      dpX[cur][j]=False
  cur=1-cur
  
if not dpX[cur][x+Xbase]:
  print("No")
  exit(0)
  
cur=0
for i in range(len(Ymove)):
  for j in range(len(dpY[cur])):
    if dpY[cur][j]:
      dpY[1-cur][j+Ymove[i]]=True
      dpY[1-cur][j-Ymove[i]]=True
      dpY[cur][j]=False
  cur=1-cur
  
if dpY[cur][y+Ybase]:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
S=input()
x,y=map(int,input().split())
Xmove=[]
Ymove=[]
xdist=True
cnt=0
for i in range(len(S)):
  if S[i]=="T":
    if xdist:
      Xmove.append(cnt)
    else:
      Ymove.append(cnt)
    cnt=0
    xdist^=True
  else:
    cnt+=1
if S[-1]=="F":
  if xdist:
    Xmove.append(cnt)
  else:
    Ymove.append(cnt)

Xbase=sum(Xmove)
Ybase=sum(Ymove)
dpX=[[False for j in range(max(Xbase,x)*2+1)] for i in range(2)]
dpY=[[False for j in range(max(Ybase,y)*2+1)] for i in range(2)]

indX=0
ind=0
if S[0]=="F":
  dpX[ind][Xbase+Xmove[0]]=True
  indX=1
else:
  dpX[ind][Xbase]=True
dpY[ind][Ybase]=True

if Xbase==0:
  if x!=0:
    print("No")
    exit(0)
else:
  for i in range(indX,len(Xmove)):
    for j in range(len(dpX[ind])):
      if dpX[ind][j]:
        dpX[1-ind][j+Xmove[i]]=True
        dpX[1-ind][j-Xmove[i]]=True
    dpX[ind]=[False for _ in range(Xbase*2+1)]
    ind=1-ind

if not dpX[ind][Xbase+x]:
  print("No")
  exit(0)
  
if Ybase==0:
  if y!=0:
    print("No")
    exit(0)
else:
  ind=0
  for i in range(len(Ymove)):
    for j in range(len(dpY[ind])):
      if dpY[ind][j]:
        dpY[1-ind][j+Ymove[i]]=True
        dpY[1-ind][j-Ymove[i]]=True
    dpY[ind]=[False for _ in range(Ybase*2+1)]
    ind=1-ind
    
print(("No","Yes")[dpY[ind][Ybase+y]])

Buggy submission (verdict: Runtime Error):
S=input()
x,y=map(int,input().split())
Xmove=[]
Ymove=[]
xdist=True
cnt=0
for i in range(len(S)):
  if S[i]=="T":
    if xdist:
      Xmove.append(cnt)
    else:
      Ymove.append(cnt)
    cnt=0
    xdist^=True
  else:
    cnt+=1
if S[-1]=="F":
  if xdist:
    Xmove.append(cnt)
  else:
    Ymove.append(cnt)

Xmax=sum(Xmove)
Ymax=sum(Ymove)
dpX=[[False for j in range(Xmax*2+2)] for i in range(len(Xmove)+1)]
dpY=[[False for j in range(Ymax*2+2)] for i in range(len(Ymove)+1)]

indX=0
if S[0]=="F":
  dpX[1][Xmax+Xmove[0]]=True
  indX=1
else:
  dpX[0][Xmax]=True
dpY[0][Ymax]=True

for i in range(indX,len(Xmove)):
  for j in range(len(dpX[i])):
    if dpX[i][j]:
      dpX[i+1][j-Xmove[i]]=True
      dpX[i+1][j+Xmove[i]]=True

if not dpX[-1][x+Xmax]:
  print("No")
  exit(0)

for i in range(len(Ymove)):
  for j in range(len(dpY[i])):
    if dpY[i][j]:
      dpY[i+1][j+Ymove[i]]=True
      dpY[i+1][j-Ymove[i]]=True

print(("No","Yes")[dpY[-1][y+Ymax]])