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: Accepted)

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T>& V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> >& Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T>& mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

//template<class Type>
//Type solve(Type res = Type()){
//    ll A,B; cin >> A >> B;
//    if(A%3 == 0 || B%3 == 0 || (A+B)%3 == 0){
//        cout << "Possible" << endl;
//    }else{
//        cout << "Impossible" << endl;
//    }
//
//    return res;
//}

//template<class Type>
//Type solve(Type res = Type()){
//    string S; cin >> S;
//    vector<ll> ans(6);
//    for(char c = 'A'; c <= 'F'; c++){
//        ans[c-'A'] = count(S.begin(),S.end(),c);
//    }
//    cout << ans << endl;
//    return res;
//}

//template<class Type>
//Type solve(Type res = Type()){
//    ll N; cin >> N;
//    vector<ll> a(N); for(auto&in:a) cin >> in;
//    ll suma = accumulate(a.begin(),a.end(),0LL);
//    if(suma%N != 0){
//        cout << -1 << endl;
//        return res;
//    }
//
//    ll eacha = suma/N;
//    ll now = a[0] - eacha;
//    for(int i = 1; i < N;i++){
//        if(now != 0){
//            res++;
//        }
//
//        now += a[i] - eacha;
//    }
//
//    cout << res << endl;
//    return res;
//}

//template<class Type>
//Type solve(Type res = Type()){
//    int N; cin >> N;
//    vector<double> R(N);
//    for(auto& in:R) cin >> in;
//    sort(R.rbegin(),R.rend());
//
//    double ans = 0;
//
//    double pi = acos(-1);
//    for(int i = 0; i < N;i++){
//        if(i%2 == 0){
//            ans += R[i]*R[i]*pi;
//        }else{
//            ans -= R[i]*R[i]*pi;
//        }
//    }
//
//    cout << fixed << setprecision(12) << ans << endl;
//    return res;
//}


//template<class Type>
//Type solve(Type res = Type()){
//
//    ll N; cin >> N;
//    ll dep = 0;
//    ll _n = N;
//    while(_n) { dep++; _n/=2; }
//    dep%=2;
//
//    bool turn = 0;
//    for(ll x = 1; x <= N;x++){
//        if(!turn){
//            if(dep){
//                x = 2*x + 1;
//            }else{
//                x = 2*x;
//            }
//        }else{
//            if(dep){
//                x = 2*x;
//            }else{
//                x = 2*x + 1;
//            }
//        }
//        turn = !turn;
//    }
//
//    if(turn){
//        cout << "Aoki" << endl;
//    }else{
//        cout << "Takahashi" << endl;
//    }
//    return res;
//}

//template<class Type>
//Type solve(Type res = Type()){
//    int N; cin >> N;
//    vector<ll> B(N);
//    for(int i = 1; i < N;i++) cin >> B[i];
//
//    vector<pll> P(N,pll(0,0));
//
//    for(int i = N-1; i > 0; i--){
//        B[i]--;
//        ll nowP = P[i].first + P[i].second + 1;
//        P[B[i]].first = max(P[B[i]].first,nowP);
//        P[B[i]].second = max(P[B[i]].second,nowP);
//    }
//
//    cout << P[0].first + P[0].second + 1 << endl;
//
//    return res;
//}

//template<class Type>
//Type solve(Type res = Type()){
//    ll N; cin >> N;
//    vector<ll> A(N); for(auto& in:A) cin >> in;
//    sort(A.begin(),A.end());
//    A.erase(unique(A.begin(),A.end()),A.end());
//    cout << A.size() << endl;
//    return res;
//}

template<class Type>
Type solve(Type res = Type()){
    string s; cin >> s;
    ll x,y; cin >> x >> y;
    
    vector<ll> vx,vy;
    
    bool turn = false;
    int cnt = 0;
    for(auto c:s){
        if(c == 'T'){
            if(!turn) vx.push_back(cnt);
            else vy.push_back(cnt);
            turn = !turn;
            cnt = 0;
        }else{
            cnt++;
        }
    }
    
    
    if(!turn) vx.push_back(cnt);
    else vy.push_back(cnt);
    
    bool dp[2][50000];
    int base;
    int cur,next;
    
    cur = 0; next = 1;
    fill(*dp,*dp+2*50000,false);
    base = 25000;
    dp[cur][base+vx[0]] = true;
    
    for(int i = 1; i < vx.size();i++){
        for(int j = 0; j < 2*base;j++){
            if(dp[cur][j]){
                dp[next][j+vx[i]] = true;
                dp[next][j-vx[i]] = true;
            }
            dp[cur][j] = false;
        }
        swap(cur,next);
    }
    if(dp[cur][base+x] == false){
        cout << "No" << endl;
        return 0;
    }
    
    cur = 0; next = 1;
    fill(*dp,*dp+2*50000,false);
    base = 25000;
    dp[cur][base] = true;
    
    for(int i = 0; i < vy.size();i++){
        for(int j = 0; j < 2*base;j++){
            if(dp[cur][j]){
                dp[next][j+vy[i]] = true;
                dp[next][j-vy[i]] = true;
            }
            dp[cur][j] = false;
        }
        swap(cur,next);
    }
    if(dp[cur][base+y] == false){
        cout << "No" << endl;
        return 0;
    }
    
    cout << "Yes" << endl;
    
    return res;
}

int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    solve(0);
    // cout << fixed << setprecision(15) << solve<string>() << endl;
    return 0;
}