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

Solution in C++ (GCC 9.2.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }
ll LCM(ll a, ll b) { return a/GCD(a, b)*b; }

string s;
int x, y;
bool dp[8080][16010];

int main() {
    cin >> s >> x >> y;
    int slen = s.length();
    vector<int> Fsum(slen+1, 0);
    for(int i = 0; i < slen; ++i) if(s[i] == 'F') Fsum[i+1]++;
    for(int i = 0; i < slen; ++i) Fsum[i+1] += Fsum[i];

    vector<int> Fd;
    int pre = 0;
    for(int i = 0; i < slen; ++i) {
        if(s[i] == 'T' || i == slen-1) {
            int fadd = Fsum[i+1] - Fsum[pre];
            Fd.emplace_back(fadd);
            pre = i;
        }
    }

    // Tの間のFの数が管理できている
    // 偶数番目の区間がx, 奇数番目の区間がy
    vector<int> Feven;
    vector<int> Fodd;
    for(int i = 0; i < (int)Fd.size(); ++i) {
        if(i % 2 == 0) Feven.emplace_back(Fd[i]);
        else Fodd.emplace_back(Fd[i]);
    }

    // cout << "kita" << endl;
    x += 8000; y += 8000;
    // xについて計算する
    memset(dp, 0, sizeof(dp));
    dp[0][8000] = true;
    int Femax = Feven.size();
    for(int i = 0; i < Femax; ++i) {
        for(int j = 0; j <= 16000; ++j) {
            if(i == 0) {
                if(j-Feven[i] >= 0) dp[i+1][j] = dp[i][j-Feven[i]];
            }
            else {
                if(j-Feven[i] >= 0 || j+Feven[i] <= 16000) {
                    dp[i+1][j] = (dp[i][j-Feven[i]] || dp[i][j+Feven[i]]);
                }
            }
        }
    }

    bool flgx = dp[Femax][x];
    /*if(flgx) cout << "true" << endl;
    else cout << "false" << endl;*/

    // yについて計算する
    memset(dp, 0, sizeof(dp));
    dp[0][8000] = true;
    int Fomax = Fodd.size();
    for(int i = 0; i < Fomax; ++i) {
        for(int j = 0; j <= 16000; ++j) {
            if(j-Fodd[i] >= 0 || j+Fodd[i] <= 16000) {
                dp[i+1][j] = (dp[i][j-Fodd[i]] || dp[i][j+Fodd[i]]);
            }
        }
    }

    bool flgy = dp[Fomax][y];
    /*if(flgy) cout << "true" << endl;
    else cout << "false" << endl;*/

    if(flgx && flgy) cout << "Yes" << endl;
    else cout << "No" << endl;
}