Problem:


Score : 500 points

Problem Statement

A robot is put at the origin in a two-dimensional plane.
Initially, the robot is facing in the positive x-axis direction.

This robot will be given an instruction sequence s.
s consists of the following two kinds of letters, and will be executed in order from front to back.

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

The objective of the robot is to be at coordinates (x, y) after all the instructions are executed.
Determine whether this objective is achievable.

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

|x|, |y| \leq |s|

Input

Input is given from Standard Input in the following format:

s
x y

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.

Sample Input 6

TTTT
1 0

Sample Output 6

No



Sample input:
FTFFTFFF
4 2


Sample output:
Yes

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc082/tasks/arc087_b


def run_length_encoding(s):
    '''
    連長圧縮を行う
    s ... iterable object e.g. list, str
    return
    ----------
    s_composed,s_num,s_idx
    それぞれ、圧縮後の文字列、その文字数、その文字が始まるidx
    '''
    s_composed = []
    s_sum = []
    s_idx = [0]
    pre = s[0]
    cnt = 1
    for i, ss in enumerate(s[1:], start=1):
        if pre == ss:
            cnt += 1
        else:
            s_sum.append(cnt)
            s_composed.append(pre)
            s_idx.append(i)
            cnt = 1
            pre = ss
    s_sum.append(cnt)
    s_composed.append(pre)
    # assert len(s_sum) == len(s_composed)
    return s_composed, s_sum, s_idx

# データに関しては事前にランレングス圧縮しておくと楽かもね


s = input()
x, y = map(int, input().split())
s_composed, s_sum, _ = run_length_encoding(s)

if s_composed[0] == 'T':  # 必ずTから始まるようにしたい
    s_composed.insert(0, 'F')
    s_sum.insert(0, 0)
# 前処理 xの移動履歴とyの移動履歴を作る
is_x = True
hist_x = []
hist_y = []
for s, n in zip(s_composed, s_sum):
    if s == 'T':
        if n & 1:  # xyの状態を反転させる
            is_x = not is_x
    else:
        if is_x:
            hist_x.append(n)
        else:
            hist_y.append(n)

'''
dpx[i][j]...x座標がjであることが可能か？ x方向の移動についてi回目の移動のあとに。
dpy[i][j]...y座標がjであることが可能か？ y方向の移動についてi回目の移動のあとに。

更新則
xについての移動,距離d移動するとすると
dpx[i+1][j+d]=dpx[i+1][j+d] or dpx[i][j]
dpx[i+1][j-d]=dpx[i+1][j-d] or dpx[i][j]
(配るDP)で更新すると楽かな？
yも同様

初期条件
dpx[:][:]=False
dpx[0][d_x0]=True #xの0回目の移動。

dpy[:][:] = False
dpy[0][d_y0]=True 簡単のために
dpy[0][-d_y0]=True
'''

if hist_x:
    x_range = 2 * (sum(hist_x) + 1500)  # ここが0になるときがある
    shift = x_range // 2
    dpx = [[False] * x_range for _ in range(len(hist_x))]
    dpx[0][hist_x[0] + shift] = True
    for i in range(1, len(hist_x)):
        d = hist_x[i]
        for j in range(x_range):
            if j + d < x_range:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j + d]
            if -1 < j - d:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j - d]
    ansx = dpx[-1][x + shift]
# else:
#     ansx = x == 0

if hist_y:
    y_range = 2 * (sum(hist_y) + 1500)  # ここが0になるときがある
    shift = y_range // 2
    dpy = [[False] * y_range for _ in range(len(hist_y))]
    dpy[0][hist_y[0] + shift] = True
    dpy[0][-hist_y[0] + shift] = True
    for i in range(1, len(hist_y)):
        d = hist_y[i]
        for j in range(y_range):
            if j + d < y_range:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j + d]
            if -1 < j - d:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j - d]
    ansy = dpy[-1][y + shift]
else:
    ansy = y == 0

print('Yes' if ansx and ansy else 'No')


# print(ansx, ansy)
# print(*dpy, sep='\n')

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc082/tasks/arc087_b


def run_length_encoding(s):
    '''
    連長圧縮を行う
    s ... iterable object e.g. list, str
    return
    ----------
    s_composed,s_num,s_idx
    それぞれ、圧縮後の文字列、その文字数、その文字が始まるidx
    '''
    s_composed = []
    s_sum = []
    s_idx = [0]
    pre = s[0]
    cnt = 1
    for i, ss in enumerate(s[1:], start=1):
        if pre == ss:
            cnt += 1
        else:
            s_sum.append(cnt)
            s_composed.append(pre)
            s_idx.append(i)
            cnt = 1
            pre = ss
    s_sum.append(cnt)
    s_composed.append(pre)
    # assert len(s_sum) == len(s_composed)
    return s_composed, s_sum, s_idx

# データに関しては事前にランレングス圧縮しておくと楽かもね


s = input()
x, y = map(int, input().split())
s_composed, s_sum, _ = run_length_encoding(s)

if s_composed[0] == 'T':  # 必ずTから始まるようにしたい
    s_composed.insert(0, 'F')
    s_sum.insert(0, 0)
# 前処理 xの移動履歴とyの移動履歴を作る
is_x = True
hist_x = []
hist_y = []
for s, n in zip(s_composed, s_sum):
    if s == 'T':
        if n & 1:  # xyの状態を反転させる
            is_x = not is_x
    else:
        if is_x:
            hist_x.append(n)
        else:
            hist_y.append(n)

'''
dpx[i][j]...x座標がjであることが可能か？ x方向の移動についてi回目の移動のあとに。
dpy[i][j]...y座標がjであることが可能か？ y方向の移動についてi回目の移動のあとに。

更新則
xについての移動,距離d移動するとすると
dpx[i+1][j+d]=dpx[i+1][j+d] or dpx[i][j]
dpx[i+1][j-d]=dpx[i+1][j-d] or dpx[i][j]
(配るDP)で更新すると楽かな？
yも同様

初期条件
dpx[:][:]=False
dpx[0][d_x0]=True #xの0回目の移動。

dpy[:][:] = False
dpy[0][d_y0]=True 簡単のために
dpy[0][-d_y0]=True
'''

if hist_x:
    x_range = 2 * (sum(hist_x) + 1000)  # ここが0になるときがある
    shift = x_range // 2
    dpx = [[False] * x_range for _ in range(len(hist_x))]
    dpx[0][hist_x[0] + shift] = True
    for i in range(1, len(hist_x)):
        d = hist_x[i]
        for j in range(x_range):
            if j + d < x_range:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j + d]
            if -1 < j - d:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j - d]
    ansx = dpx[-1][x + shift]
# else:
#     ansx = x == 0

if hist_y:
    y_range = 2 * (sum(hist_y) + 1000)  # ここが0になるときがある
    shift = y_range // 2
    dpy = [[False] * y_range for _ in range(len(hist_y))]
    dpy[0][hist_y[0] + shift] = True
    dpy[0][-hist_y[0] + shift] = True
    for i in range(1, len(hist_y)):
        d = hist_y[i]
        for j in range(y_range):
            if j + d < y_range:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j + d]
            if -1 < j - d:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j - d]
    ansy = dpy[-1][y + shift]
else:
    ansy = y == 0

print('Yes' if ansx and ansy else 'No')


# print(ansx, ansy)
# print(*dpy, sep='\n')

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc082/tasks/arc087_b


def run_length_encoding(s):
    '''
    連長圧縮を行う
    s ... iterable object e.g. list, str
    return
    ----------
    s_composed,s_num,s_idx
    それぞれ、圧縮後の文字列、その文字数、その文字が始まるidx
    '''
    s_composed = []
    s_sum = []
    s_idx = [0]
    pre = s[0]
    cnt = 1
    for i, ss in enumerate(s[1:], start=1):
        if pre == ss:
            cnt += 1
        else:
            s_sum.append(cnt)
            s_composed.append(pre)
            s_idx.append(i)
            cnt = 1
            pre = ss
    s_sum.append(cnt)
    s_composed.append(pre)
    # assert len(s_sum) == len(s_composed)
    return s_composed, s_sum, s_idx

# データに関しては事前にランレングス圧縮しておくと楽かもね


s = input()
x, y = map(int, input().split())
s_composed, s_sum, _ = run_length_encoding(s)

if s_composed[0] == 'T':  # 必ずTから始まるようにしたい
    s_composed.insert(0, 'F')
    s_sum.insert(0, 0)
# 前処理 xの移動履歴とyの移動履歴を作る
is_x = True
hist_x = []
hist_y = []
for s, n in zip(s_composed, s_sum):
    if s == 'T':
        if n & 1:  # xyの状態を反転させる
            is_x = not is_x
    else:
        if is_x:
            hist_x.append(n)
        else:
            hist_y.append(n)

'''
dpx[i][j]...x座標がjであることが可能か？ x方向の移動についてi回目の移動のあとに。
dpy[i][j]...y座標がjであることが可能か？ y方向の移動についてi回目の移動のあとに。

更新則
xについての移動,距離d移動するとすると
dpx[i+1][j+d]=dpx[i+1][j+d] or dpx[i][j]
dpx[i+1][j-d]=dpx[i+1][j-d] or dpx[i][j]
(配るDP)で更新すると楽かな？
yも同様

初期条件
dpx[:][:]=False
dpx[0][d_x0]=True #xの0回目の移動。

dpy[:][:] = False
dpy[0][d_y0]=True 簡単のために
dpy[0][-d_y0]=True
'''

if hist_x:
    x_range = 2 * (sum(hist_x) + 100 + max(hist_x))  # ここが0になるときがある
    shift = x_range // 2
    dpx = [[False] * x_range for _ in range(len(hist_x))]
    dpx[0][hist_x[0] + shift] = True
    for i in range(1, len(hist_x)):
        d = hist_x[i]
        for j in range(x_range):
            if j + d < x_range:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j + d]
            if -1 < j - d:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j - d]
    ansx = dpx[-1][x + shift]
# else:
#     ansx = x == 0

if hist_y:
    y_range = 2 * (sum(hist_y) + 100 + max(hist_y))  # ここが0になるときがある
    shift = y_range // 2
    dpy = [[False] * y_range for _ in range(len(hist_y))]
    dpy[0][hist_y[0] + shift] = True
    dpy[0][-hist_y[0] + shift] = True
    for i in range(1, len(hist_y)):
        d = hist_y[i]
        for j in range(y_range):
            if j + d < y_range:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j + d]
            if -1 < j - d:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j - d]
    ansy = dpy[-1][y + shift]
else:
    ansy = y == 0

print('Yes' if ansx and ansy else 'No')


# print(ansx, ansy)
# print(*dpy, sep='\n')

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc082/tasks/arc087_b


def run_length_encoding(s):
    '''
    連長圧縮を行う
    s ... iterable object e.g. list, str
    return
    ----------
    s_composed,s_num,s_idx
    それぞれ、圧縮後の文字列、その文字数、その文字が始まるidx
    '''
    s_composed = []
    s_sum = []
    s_idx = [0]
    pre = s[0]
    cnt = 1
    for i, ss in enumerate(s[1:], start=1):
        if pre == ss:
            cnt += 1
        else:
            s_sum.append(cnt)
            s_composed.append(pre)
            s_idx.append(i)
            cnt = 1
            pre = ss
    s_sum.append(cnt)
    s_composed.append(pre)
    # assert len(s_sum) == len(s_composed)
    return s_composed, s_sum, s_idx

# データに関しては事前にランレングス圧縮しておくと楽かもね


s = input()
x, y = map(int, input().split())
s_composed, s_sum, _ = run_length_encoding(s)

if s_composed[0] == 'T':  # 必ずTから始まるようにしたい
    s_composed.insert(0, 'F')
    s_sum.insert(0, 0)
# 前処理 xの移動履歴とyの移動履歴を作る
is_x = True
hist_x = []
hist_y = []
for s, n in zip(s_composed, s_sum):
    if s == 'T':
        if n & 1:  # xyの状態を反転させる
            is_x = not is_x
    else:
        if is_x:
            hist_x.append(n)
        else:
            hist_y.append(n)

'''
dpx[i][j]...x座標がjであることが可能か？ x方向の移動についてi回目の移動のあとに。
dpy[i][j]...y座標がjであることが可能か？ y方向の移動についてi回目の移動のあとに。

更新則
xについての移動,距離d移動するとすると
dpx[i+1][j+d]=dpx[i+1][j+d] or dpx[i][j]
dpx[i+1][j-d]=dpx[i+1][j-d] or dpx[i][j]
(配るDP)で更新すると楽かな？
yも同様

初期条件
dpx[:][:]=False
dpx[0][d_x0]=True #xの0回目の移動。

dpy[:][:] = False
dpy[0][d_y0]=True 簡単のために
dpy[0][-d_y0]=True
'''

# if hist_x:
#     x_range = 2 * sum(hist_x) + 100
#     shift = x_range // 2
#     dpx = [[False] * x_range for _ in range(len(hist_x))]
#     dpx[0][hist_x[0] + shift] = True
#     for i in range(1, len(hist_x)):
#         d = hist_x[i]
#         for j in range(x_range):
#             if j + d < x_range:
#                 dpx[i][j] = dpx[i][j] or dpx[i - 1][j + d]
#             if -1 < j - d:
#                 dpx[i][j] = dpx[i][j] or dpx[i - 1][j - d]
#     ansx = dpx[-1][x + shift]

if hist_y:
    y_range = 2 * sum(hist_y) + 100
    shift = y_range // 2
    dpy = [[False] * y_range for _ in range(len(hist_y))]
    dpy[0][hist_y[0] + shift] = True
    dpy[0][-hist_y[0] + shift] = True
    for i in range(1, len(hist_y)):
        d = hist_y[i]
        for j in range(y_range):
            if j + d < y_range:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j + d]
            if -1 < j - d:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j - d]
    ansy = dpy[-1][y + shift]
else:
    ansy = True if y == 0 else False

# print('Yes' if ansx and ansy else 'No')


# print(ansx, ansy)
# print(*dpy, sep='\n')

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc082/tasks/arc087_b


def run_length_encoding(s):
    '''
    連長圧縮を行う
    s ... iterable object e.g. list, str
    return
    ----------
    s_composed,s_num,s_idx
    それぞれ、圧縮後の文字列、その文字数、その文字が始まるidx
    '''
    s_composed = []
    s_sum = []
    s_idx = [0]
    pre = s[0]
    cnt = 1
    for i, ss in enumerate(s[1:], start=1):
        if pre == ss:
            cnt += 1
        else:
            s_sum.append(cnt)
            s_composed.append(pre)
            s_idx.append(i)
            cnt = 1
            pre = ss
    s_sum.append(cnt)
    s_composed.append(pre)
    # assert len(s_sum) == len(s_composed)
    return s_composed, s_sum, s_idx

# データに関しては事前にランレングス圧縮しておくと楽かもね


s = input()
x, y = map(int, input().split())
s_composed, s_sum, _ = run_length_encoding(s)

if s_composed[0] == 'T':  # 必ずTから始まるようにしたい
    s_composed.insert(0, 'F')
    s_sum.insert(0, 0)
# 前処理 xの移動履歴とyの移動履歴を作る
is_x = True
hist_x = []
hist_y = []
for s, n in zip(s_composed, s_sum):
    if s == 'T':
        if n & 1:  # xyの状態を反転させる
            is_x = not is_x
    else:
        if is_x:
            hist_x.append(n)
        else:
            hist_y.append(n)

'''
dpx[i][j]...x座標がjであることが可能か？ x方向の移動についてi回目の移動のあとに。
dpy[i][j]...y座標がjであることが可能か？ y方向の移動についてi回目の移動のあとに。

更新則
xについての移動,距離d移動するとすると
dpx[i+1][j+d]=dpx[i+1][j+d] or dpx[i][j]
dpx[i+1][j-d]=dpx[i+1][j-d] or dpx[i][j]
(配るDP)で更新すると楽かな？
yも同様

初期条件
dpx[:][:]=False
dpx[0][d_x0]=True #xの0回目の移動。

dpy[:][:] = False
dpy[0][d_y0]=True 簡単のために
dpy[0][-d_y0]=True
'''

if hist_x:
    x_range = 2 * sum(hist_x) + 100
    shift = x_range // 2
    dpx = [[False] * x_range for _ in range(len(hist_x))]
    dpx[0][hist_x[0] + shift] = True
    for i in range(1, len(hist_x)):
        d = hist_x[i]
        for j in range(x_range):
            if j + d < x_range:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j + d]
            if -1 < j - d:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j - d]
    ansx = dpx[-1][x + shift]

# if hist_y:
#     y_range = 2 * sum(hist_y) + 100
#     shift = y_range // 2
#     dpy = [[False] * y_range for _ in range(len(hist_y))]
#     dpy[0][hist_y[0] + shift] = True
#     dpy[0][-hist_y[0] + shift] = True
#     for i in range(1, len(hist_y)):
#         d = hist_y[i]
#         for j in range(y_range):
#             if j + d < y_range:
#                 dpy[i][j] = dpy[i][j] or dpy[i - 1][j + d]
#             if -1 < j - d:
#                 dpy[i][j] = dpy[i][j] or dpy[i - 1][j - d]
#     ansy = dpy[-1][y + shift]
# else:
#     ansy = True if y == 0 else False

# print('Yes' if ansx and ansy else 'No')


# print(ansx, ansy)
# print(*dpy, sep='\n')

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc082/tasks/arc087_b


def run_length_encoding(s):
    '''
    連長圧縮を行う
    s ... iterable object e.g. list, str
    return
    ----------
    s_composed,s_num,s_idx
    それぞれ、圧縮後の文字列、その文字数、その文字が始まるidx
    '''
    s_composed = []
    s_sum = []
    s_idx = [0]
    pre = s[0]
    cnt = 1
    for i, ss in enumerate(s[1:], start=1):
        if pre == ss:
            cnt += 1
        else:
            s_sum.append(cnt)
            s_composed.append(pre)
            s_idx.append(i)
            cnt = 1
            pre = ss
    s_sum.append(cnt)
    s_composed.append(pre)
    # assert len(s_sum) == len(s_composed)
    return s_composed, s_sum, s_idx

# データに関しては事前にランレングス圧縮しておくと楽かもね


s = input()
x, y = map(int, input().split())
s_composed, s_sum, _ = run_length_encoding(s)

if s_composed[0] == 'T':  # 必ずTから始まるようにしたい
    s_composed.insert(0, 'F')
    s_sum.insert(0, 0)
# 前処理 xの移動履歴とyの移動履歴を作る
is_x = True
hist_x = []
hist_y = []
for s, n in zip(s_composed, s_sum):
    if s == 'T':
        if n & 1:  # xyの状態を反転させる
            is_x = not is_x
    else:
        if is_x:
            hist_x.append(n)
        else:
            hist_y.append(n)

'''
dpx[i][j]...x座標がjであることが可能か？ x方向の移動についてi回目の移動のあとに。
dpy[i][j]...y座標がjであることが可能か？ y方向の移動についてi回目の移動のあとに。

更新則
xについての移動,距離d移動するとすると
dpx[i+1][j+d]=dpx[i+1][j+d] or dpx[i][j]
dpx[i+1][j-d]=dpx[i+1][j-d] or dpx[i][j]
(配るDP)で更新すると楽かな？
yも同様

初期条件
dpx[:][:]=False
dpx[0][d_x0]=True #xの0回目の移動。

dpy[:][:] = False
dpy[0][d_y0]=True 簡単のために
dpy[0][-d_y0]=True
'''

if hist_x:
    x_range = 2 * sum(hist_x) + 200
    shift = x_range // 2
    dpx = [[False] * x_range for _ in range(len(hist_x) + 2)]
    dpx[0][hist_x[0] + shift] = True
    for i in range(1, len(hist_x)):
        d = hist_x[i]
        for j in range(x_range):
            if j + d < x_range:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j + d]
            if -1 < j - d:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j - d]
    ansx = dpx[-3][x + shift]

if hist_y:
    y_range = 2 * sum(hist_y) + 200
    shift = y_range // 2
    dpy = [[False] * y_range for _ in range(len(hist_y) + 2)]
    dpy[0][hist_y[0] + shift] = True
    dpy[0][-hist_y[0] + shift] = True
    for i in range(1, len(hist_y)):
        d = hist_y[i]
        for j in range(y_range):
            if j + d < y_range:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j + d]
            if -1 < j - d:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j - d]
    ansy = dpy[-3][y + shift]
else:
    ansy = True if y == 0 else False

print('Yes' if ansx and ansy else 'No')
# print(ansx, ansy)
# print(*dpy, sep='\n')

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc082/tasks/arc087_b


def run_length_encoding(s):
    '''
    連長圧縮を行う
    s ... iterable object e.g. list, str
    return
    ----------
    s_composed,s_num,s_idx
    それぞれ、圧縮後の文字列、その文字数、その文字が始まるidx
    '''
    s_composed = []
    s_sum = []
    s_idx = [0]
    pre = s[0]
    cnt = 1
    for i, ss in enumerate(s[1:], start=1):
        if pre == ss:
            cnt += 1
        else:
            s_sum.append(cnt)
            s_composed.append(pre)
            s_idx.append(i)
            cnt = 1
            pre = ss
    s_sum.append(cnt)
    s_composed.append(pre)
    # assert len(s_sum) == len(s_composed)
    return s_composed, s_sum, s_idx

# データに関しては事前にランレングス圧縮しておくと楽かもね


s = input()
x, y = map(int, input().split())
s_composed, s_sum, _ = run_length_encoding(s)

if s_composed[0] == 'T':  # 必ずTから始まるようにしたい
    s_composed.insert(0, 'F')
    s_sum.insert(0, 0)
# 前処理 xの移動履歴とyの移動履歴を作る
is_x = True
hist_x = []
hist_y = []
for s, n in zip(s_composed, s_sum):
    if s == 'T':
        if n & 1:  # xyの状態を反転させる
            is_x = not is_x
    else:
        if is_x:
            hist_x.append(n)
        else:
            hist_y.append(n)

'''
dpx[i][j]...x座標がjであることが可能か？ x方向の移動についてi回目の移動のあとに。
dpy[i][j]...y座標がjであることが可能か？ y方向の移動についてi回目の移動のあとに。

更新則
xについての移動,距離d移動するとすると
dpx[i+1][j+d]=dpx[i+1][j+d] or dpx[i][j]
dpx[i+1][j-d]=dpx[i+1][j-d] or dpx[i][j]
(配るDP)で更新すると楽かな？
yも同様

初期条件
dpx[:][:]=False
dpx[0][d_x0]=True #xの0回目の移動。

dpy[:][:] = False
dpy[0][d_y0]=True 簡単のために
dpy[0][-d_y0]=True
'''

if hist_x:
    x_range = 2 * sum(hist_x) + 100
    shift = x_range // 2
    dpx = [[False] * x_range for _ in range(len(hist_x))]
    dpx[0][hist_x[0] + shift] = True
    for i in range(1, len(hist_x)):
        d = hist_x[i]
        for j in range(x_range):
            if j + d < x_range:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j + d]
            if -1 < j - d:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j - d]
    ansx = dpx[-1][x + shift]

if hist_y:
    y_range = 2 * sum(hist_y) + 100
    shift = y_range // 2
    dpy = [[False] * y_range for _ in range(len(hist_y))]
    dpy[0][hist_y[0] + shift] = True
    dpy[0][-hist_y[0] + shift] = True
    for i in range(1, len(hist_y)):
        d = hist_y[i]
        for j in range(y_range):
            if j + d < y_range:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j + d]
            if -1 < j - d:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j - d]
            # dpy[i][j + d] = dpy[i][j + d] or dpy[i - 1][j]
            # dpy[i][j - d] = dpy[i][j - d] or dpy[i - 1][j]
    ansy = dpy[-1][y + shift]
else:
    ansy = True if y == 0 else False

print('Yes' if ansx and ansy else 'No')
# print(ansx, ansy)
# print(*dpy, sep='\n')

Submission status: Runtime Error

Solution code:
import numpy as np

s = input()
x_goal, y_goal = map(int, input().split())

directed_x = None
steps = 0
xs = [0]
ys = [0]
for c in (s+'T'):   #sentinel
    if c == 'F':
        steps += 1
    else:
        if directed_x is None:
            x_goal -= steps
            directed_x = True
        elif steps != 0:
            if directed_x:
                xs.append(steps)
            else:
                ys.append(steps)
        steps = 0
        directed_x = not directed_x

dx_2 = sum(xs) - x_goal
if dx_2 % 2 == 1:
    ans = False
else:
    dx = dx_2 // 2
    possible_x = np.full(dx + 1, False)
    possible_x[0] = True
    for x in xs:
        possible_x[x:] = np.logical_or(possible_x[:len(possible_x)-x], possible_x[x:])
        if possible_x[dx]:
            ans = True
            break
    else:
        ans = False

if ans:
    dy_2 = sum(ys) - y_goal
    if dy_2 % 2 == 1:
        ans = False
    else:
        dy = dy_2 // 2
        possible_y = np.full(dy + 1, False)
        possible_y[0] = True
        for y in ys:
            possible_y[y:] = np.logical_or(possible_y[:len(possible_y)-y], possible_y[y:])
            if possible_y[dy]:
                ans = True
                break
        else:
            ans = False

if ans:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    s=list(input())+['T']
    x,y=map(int,input().split())
    x+=10000
    y+=10000
    ud=[]
    lr=[]
    f=0
    f2=0
    c=0
    for t in s:
        if t=='F':
            if not f:
                x-=1
            else:
                c+=1
        else:
            f=1
            if c:
                if f2:
                    ud.append(c)
                else:
                    lr.append(c)
            c=0
            f2=not f2
    for l,z in [(ud,y),(lr,x)]:
        N=len(l)
        dp=[0]*(N+1)
        dp[0]=1<<10000
        for i in range(N):
            dp[i+1]|=dp[i]<<l[i]|dp[i]>>l[i]
        if not (dp[-1]>>z)&1:
            print('No')
            break
    else:
        print('Yes')
        
    

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    s=list(input())+['T']
    x,y=map(int,input().split())
    x+=8000
    y+=8000
    ud=[]
    lr=[]
    f=0
    f2=0
    c=0
    for t in s:
        if t=='F':
            if not f:
                x-=1
            else:
                c+=1
        else:
            f=1
            if c:
                if f2:
                    ud.append(c)
                else:
                    lr.append(c)
            c=0
            f2=not f2
    for l,z in [(ud,y),(lr,x)]:
        N=len(l)
        dp=[0]*(N+1)
        dp[0]=1<<8000
        for i in range(N):
            dp[i+1]|=dp[i]<<l[i]|dp[i]>>l[i]
        if not (dp[-1]>>z)&1:
            print('No')
            break
    else:
        print('Yes')
        
    

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
S = input()
X, Y = map(int, input().split())

if S.find("F") < 0:
    if X != 0 or Y != 0:
        print("No")
    else:
        print("Yes")
    exit()

n = len(S)
d = list(map(len, S.split("T")))
xs = d[::2]
ys = d[1::2]

dpx = [set() for _ in range(len(xs))]
dpx[0].add(0)
for i in range(len(xs)):
    for j in dpx[i]:
        dpx[i + 1].add(j + xs[i])
        dpx[i + 1].add(j - xs[i])

dpy = [set() for _ in range(len(ys) + 1)]
dpy[0].add(0)
for i in range(len(ys)):
    for j in dpy[i]:
        dpy[i + 1].add(j + ys[i])
        dpy[i + 1].add(j - ys[i])

ans = (X in dpx[-1]) & (Y in dpy[-1])
print("Yes" if ans else "No")

Submission status: Runtime Error

Solution code:
S = input()
X, Y = map(int, input().split())

n = len(S)
xs = list(map(len, S.split("T")[::2]))
ys = list(map(len, S.split("T")[1::2]))

dpx = [[False] * (2 * n + 1) for _ in range(len(xs))]
for i in range(len(xs)):
    if i == 0:
        d = (-1) ** S.find("F")
        dpx[i][d * xs[i] + n] = True
        continue

    for j in range(2 * n + 1):
        if j + xs[i] <= 2 * n:
            dpx[i][j] |= dpx[i - 1][j + xs[i]]
        if 0 <= j - xs[i] <= 2 * n:
            dpx[i][j] |= dpx[i - 1][j - xs[i]]

ys = [0] + ys
dpy = [[False] * (2 * n + 1) for _ in range(len(ys))]
for i in range(len(ys)):
    if i == 0:
        dpy[i][n] = True
        continue

    for j in range(2 * n + 1):
        if j + ys[i] <= 2 * n:
            dpy[i][j] |= dpy[i - 1][j + ys[i]]
        if 0 <= j - ys[i] <= 2 * n:
            dpy[i][j] |= dpy[i - 1][j - ys[i]]

ans = dpx[-1][X + n] & dpy[-1][Y + n]
print("Yes" if ans else "No")

Submission status: Runtime Error

Solution code:
s = input()
x,y = map(int,input().split())
cur_x = 0
cur_y = 0
L_x = []
L_y = []
s += 'K'
temp = 0
dirc = 'yoko'
for i in range(len(s)):
    if s[i] == 'F':
        temp += 1
    elif s[i] == 'T':
        if temp == 0:
            if dirc == 'yoko':
                dirc = 'tate'
            else:
                dirc = 'yoko'
        else:
            if dirc == 'yoko':
                L_x.append(temp)
                temp = 0
                dirc = 'tate'
            else:
                L_y.append(temp)
                temp = 0
                dirc = 'yoko'
    else:
        if temp != 0:
            if dirc == 'yoko':
                L_x.append(temp)
                temp = 0
                dirc = 'tate'
            else:
                L_y.append(temp)
                temp = 0
                dirc = 'yoko'
dp_x = [[False]*8001 for i in range(len(L_x)+1)]
dp_y = [[False]*8001 for i in range(len(L_y)+1)]
dp_x[0][0] = True
dp_y[0][0] = True
if s[0] != 'F':
    for i in range(1,len(L_x)+1):
        for j in range(8001):
            if j+L_x[i-1] <= 8000:
                dp_x[i][j] = dp_x[i-1][abs(j-L_x[i-1])] or dp_x[i-1][j+L_x[i-1]]
            else:
                dp_x[i][j] = dp_x[i-1][abs(j-L_x[i-1])]
    for i in range(1,len(L_y)+1):
        for j in range(8001):
            if j+L_y[i-1] <= 8000:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])] or dp_y[i-1][j+L_y[i-1]]
            else:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])]
    if dp_x[len(L_x)][abs(x)] == True and dp_y[len(L_y)][abs(y)] == True:
        print('Yes')
    else:
        print('No')
else:
    for i in range(2,len(L_x)+1):
        for j in range(8001):
            if j+L_x[i-1] <= 8000:
                dp_x[i-1][j] = dp_x[i-2][abs(j-L_x[i-1])] or dp_x[i-2][j+L_x[i-1]]
            else:
                dp_x[i-1][j] = dp_x[i-2][abs(j-L_x[i-1])]
    for i in range(1,len(L_y)+1):
        for j in range(8001):
            if j+L_y[i-1] <= 8000:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])] or dp_y[i-1][j+L_y[i-1]]
            else:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])]
    if 1 == 2
        print('Yes')
    else:
        print('No')

Submission status: Runtime Error

Solution code:
s = input()
x,y = map(int,input().split())
cur_x = 0
cur_y = 0
L_x = []
L_y = []
s += 'K'
temp = 0
dirc = 'yoko'
for i in range(len(s)):
    if s[i] == 'F':
        temp += 1
    elif s[i] == 'T':
        if temp == 0:
            if dirc == 'yoko':
                dirc = 'tate'
            else:
                dirc = 'yoko'
        else:
            if dirc == 'yoko':
                L_x.append(temp)
                temp = 0
                dirc = 'tate'
            else:
                L_y.append(temp)
                temp = 0
                dirc = 'yoko'
    else:
        if temp != 0:
            if dirc == 'yoko':
                L_x.append(temp)
                temp = 0
                dirc = 'tate'
            else:
                L_y.append(temp)
                temp = 0
                dirc = 'yoko'
dp_x = [[False]*8001 for i in range(len(L_x)+1)]
dp_y = [[False]*8001 for i in range(len(L_y)+1)]
dp_x[0][0] = True
dp_y[0][0] = True
if s[0] != 'F':
    for i in range(1,len(L_x)+1):
        for j in range(8001):
            if j+L_x[i-1] <= 8000:
                dp_x[i][j] = dp_x[i-1][abs(j-L_x[i-1])] or dp_x[i-1][j+L_x[i-1]]
            else:
                dp_x[i][j] = dp_x[i-1][abs(j-L_x[i-1])]
    for i in range(1,len(L_y)+1):
        for j in range(8001):
            if j+L_y[i-1] <= 8000:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])] or dp_y[i-1][j+L_y[i-1]]
            else:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])]
    if dp_x[len(L_x)][abs(x)] == True and dp_y[len(L_y)][abs(y)] == True:
        print('Yes')
    else:
        print('No')
else:
    for i in range(2,len(L_x)+1):
        for j in range(8001):
            if j+L_x[i-1] <= 8000 and abs(j-L_x[i-1]) <= 8000:
                dp_x[i-1][j] = dp_x[i-2][abs(j-L_x[i-1])] or dp_x[i-2][j+L_x[i-1]]
            elif abs(j-L_x[i-1]) <= 8000:
                dp_x[i-1][j] = dp_x[i-2][abs(j-L_x[i-1])]
    for i in range(1,len(L_y)+1):
        for j in range(8001):
            if j+L_y[i-1] <= 8000:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])] or dp_y[i-1][j+L_y[i-1]]
            else:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])]
    if dp_x[len(L_x)-1][abs(x-L_x[0])] == True and dp_y[len(L_y)][abs(y)] == True:
        print('Yes')
    else:
        print('No')