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>
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define reprrev(i,a,b) for(int i=b-1;i>=a;i--) // [a, b)
#define reprev(i,n) reprrev(i,0,n)
typedef long long ll;
typedef unsigned long long ull;
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; }

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
    sizeは(int)とキャストしよう
    ごちゃごちゃ場合分けを考える前に全探索は考えましたか？
    詰まった時に、別の分野の問題として考え直す（これdpでは？、グラフとしてみればいいのでは？）
    多くの問題はパターンマッチだが、パターンに落とし込むまでが難しく、そのための訓練としてわからない問題をあれこれ色々な角度から考察してみるのではないか
    cin.tie(0);
    ios::sync_with_stdio(false);<- これら、printfとかと併用しない方が良さそう

*/

const ll mod = 1e9+7;

void chmod(ll &M){
    if(M >= mod) M %= mod;
    else if(M < 0){
        M += (abs(M)/mod + 1)*mod;
        M %= mod;
    }
}

ll modpow(ll x, ll n){
    if(n==0) return 1;
    ll res=modpow(x, n/2);

    if(n%2==0) return res*res%mod;
    else return res*res%mod*x%mod;
}

int getl(int i, int N) { return i==0? N-1:i-1; };
int getr(int i, int N) { return i==N-1? 0:i+1; };

/* <--------------------------------------------> */

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

    string s; cin >> s;
    int n=s.size();
    int x, y; cin >> x >> y;
    vector<int> tate, yoko;

    int pos=0, fcnt=0, tcnt=0;
    while(pos<n){
        if(s[pos]=='T') { ++fcnt; ++pos; }
        else{
            tcnt=1;
            while(pos<n && s[pos+1]=='F') { ++pos; ++tcnt; }
            if(fcnt%2==0) yoko.push_back(tcnt);
            else tate.push_back(tcnt);
            ++pos;
        }
    }

    bool first_right= (s[0]=='F'? true:false);
    // rep(i, yoko.size()) cout << yoko[i] << ' ';
    // cout << endl;
    // rep(i, tate.size()) cout << tate[i] << ' ';
    // cout << endl;

    int base=8010;
    int lim=base*2;
    vector<vector<bool>> dpyoko(2, vector<bool>(lim, false)), dptate(2, vector<bool>(lim, false));
    
    dpyoko[0][base]=true;
    rep(i, yoko.size()) rep(j, lim){
        dpyoko[(i+2)%2][j]=true;
        if( (j-yoko[i]>=0) && (dpyoko[i%2][j-yoko[i]]) )  dpyoko[(i+1)%2][j]=true;

        if( !(i==0&&first_right) && (j+yoko[i]<lim) && (dpyoko[i%2][j+yoko[i]]) ) dpyoko[(i+1)%2][j]=true;
    }

    dptate[0][base]=true;
    rep(i, tate.size()) rep(j, lim){
        dptate[(i+1)%2][j]=false;
        if(j-tate[i]>=0 && dptate[i%2][j-tate[i]]) dptate[(i+1)%2][j]=true;

        if(j+tate[i]<lim && dptate[i%2][j+tate[i]]) dptate[(i+1)%2][j]=true;
    }

    if(dpyoko[yoko.size()%2][x+base] && dptate[tate.size()%2][y+base]) cout << "Yes\n"s;
    else cout << "No\n"s;

    return 0;
}