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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define LCM(a, b) (a) / __gcd((a), (b)) * (b)
#define CEIL(a, b) (a)/(b)+(((a)%(b))?1:0)
#define ln '\n'

using namespace std;
using LL = long long;
using ldouble = long double;
using P = pair<int, int>;
using LP = pair<LL, LL>;

static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1e9 + 7;
static const int SIZE = 200005;

const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};

vector<LL> Div(LL n) {
    vector<LL> ret;
    for(LL i = 1; i * i <= n; ++i) {
        if(n % i == 0) {
            ret.pb(i);
            if(i * i != n) ret.pb(n / i);
        }
    }
    sort(all(ret));
    return ret;
}

bool dp_min[4005][8005], dp_pl[4005][8005];

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

    string s;
    cin >> s;
    int x, y;
    cin >> x >> y;

    vector<int> ver, hor;
    bool f = true;
    int c = 0;
    int len = s.size();
    for(int i = 0; i < len; ++i) {
        if(s[i] == 'T') {
            if(f) {
                if(hor.empty()) {
                    dp_pl[1][c] = true;
                }
                hor.pb(c);
                f = false;
            } else {
                ver.pb(c);
                f = true;
            }
            c = 0;
        } else {
            ++c;
        }
    }

    if(f) {
        if(hor.empty()) dp_pl[1][c] = true;
        hor.pb(c);
    } else {
        ver.pb(c);
    }

    for(int i = 1; i < hor.size(); ++i) {
        for(int j = 0; j <= 8000; ++j) {
            if(dp_pl[i][j]) {
                if(j - hor[i] < 0) {
                    dp_min[i + 1][hor[i] - j] = true;
                } else {
                    dp_pl[i + 1][j - hor[i]] = true;
                }
                dp_pl[i + 1][j + hor[i]] = true;
            }
            if(dp_min[i][j]) {
                dp_min[i + 1][j + hor[i]] = true;
                if(j - hor[i] < 0) {
                    dp_pl[i + 1][hor[i] - j] = true;
                } else {
                    dp_min[i + 1][j - hor[i]] = true;
                }
            }
        }
    }


    if(x > 0 && !dp_pl[hor.size()][x] ||
        x < 0 && !dp_min[hor.size()][-x] ||
         x == 0 && !dp_pl[hor.size()][0] && !dp_min[hor.size()][0]) {
        cout << "No" << endl;
        return 0;
    }

    for(int i = 1; i <= hor.size(); ++i) {
        for(int j = 0; j <= 8000; ++j) {
            dp_pl[i][j] = dp_min[i][j] = false;
        }
    }

    dp_pl[0][0] = dp_min[0][0] = true;
    for(int i = 0; i < ver.size(); ++i) {
        for(int j = 0; j <= 8000; ++j) {
            if(dp_pl[i][j]) {
                if(j - ver[i] < 0) {
                    dp_min[i + 1][ver[i] - j] = true;
                } else {
                    dp_pl[i + 1][j - ver[i]] = true;
                }
                dp_pl[i + 1][j + ver[i]] = true;
            }
            if(dp_min[i][j]) {
                dp_min[i + 1][j + ver[i]] = true;
                if(j - hor[i] < 0) {
                    dp_pl[i + 1][ver[i] - j] = true;
                } else {
                    dp_min[i + 1][j - ver[i]] = true;
                }
            }
        }
    }

    if(y > 0 && !dp_pl[ver.size()][y] ||
        y < 0 && !dp_min[ver.size()][-y] ||
         y == 0 && !dp_pl[ver.size()][0] && !dp_min[ver.size()][0]) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }

    return 0;
}