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>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<typename T> inline bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T> inline bool chmin(T &a,T b){if(b<a){a=b;return 1;}return 0;}
template<typename T> vector<T> make_vec(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_vec(size_t a,Ts... ts){return vector<decltype(make_vec<T>(ts...))>(a,make_vec<T>(ts...));}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T,U>::value>::type fill_v(U &u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<!is_same<T,U>::value>::type fill_v(U &u,const V... v){for(auto &e:u)fill_v<T>(e,v...);}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    int gx, gy;
    cin >> s >> gx >> gy;
    int n = sz(s);
    vector<int> x, y;
    int sx = 0, sy = 0;
    vector<pair<char, int>> v;
    v.emplace_back(s[0], 0);
    rep(i, n) {
        if (v.back().fi == s[i]) v.back().se++;
        else v.emplace_back(s[i], 1);
    }
    bool horizontal = true;
    rep(i, sz(v)) {
        if (i == 0 && v[i].fi == 'F') sx += v[i].se;
        else if (v[i].fi == 'T') horizontal ^= (v[i].se % 2);
        else {
            if (horizontal) x.push_back(v[i].se);
            else y.push_back(v[i].se);
        }
    }
    auto dp = make_vec<bool>(sz(x)+1, 20000);
    dp[0][10000] = true;
    rep(i, sz(x)) {
        rep(j, 20000) {
            if (dp[i][j]) {
                dp[i+1][j-x[i]] = true;
                dp[i+1][j+x[i]] = true;
            }
        }
    }
    int dx = gx - sx;
    if (!dp[sz(x)][dx+10000]) {
        puts("No");
        return 0;
    }
    dp = make_vec<bool>(sz(y)+1, 20000);
    dp[0][10000] = true;
    rep(i, sz(y)) {
        rep(j, 20000) {
            if (dp[i][j]) {
                dp[i+1][j-y[i]] = true;
                dp[i+1][j+y[i]] = true;
            }
        }
    }
    int dy = gy - sy;
    if (!dp[sz(y)][dy+10000]) {
        puts("No");
        return 0;
    }
    puts("Yes");
    return 0;
}