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

#include "bits/stdc++.h"
#include <chrono>
#include <random>
#include <cstdio>
#define ll long long int
#define endl '\n'
#define pb push_back
#define mp make_pair
#define mod 998244353LL
#define rep(i,n) for(ll i=0;i<n;i++)
#define vi vector <int>
#define vs vector <string>
#define vc vector <char>
#define vl vector <ll>
#define pll pair<ll, ll>
#define pint pair<int, int>
#define S second
#define F first
#define all(c) (c).begin(),(c).end()
using namespace std;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500001;
ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } 
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
#define pi 3.14159265358979323846264338
using namespace std; 
ll max(ll a, ll b){ if(a>b)return a;return b;}
ll min(ll a, ll b){ if(a < b) return a;return b;}
ll inf = 100000000000000000;

void solve() 
{
    string s;
    cin>>s;

    int x, y;
    cin>>x>>y;

    vi d, xv, yv;
    d.pb(-1);

    int cnt = 0;

    rep(i, s.size())
    {
        if(s[i] == 'F') cnt++;
        else
        {
            d.pb(cnt);
            cnt = 0;
        }
    }
    if(cnt > 0) d.pb(cnt);

    for(int i = 2; i < d.size(); i++)
    {
        if(i%2 == 0) yv.pb(d[i]);
        else xv.pb(d[i]);
    }

    int n = d.size();

    bool dp[n+1][16000];

    rep(i, n+1)
    {
        rep(j, 16000) dp[i][j] = false;
    }

    dp[0][8000] = true;
    dp[1][d[1] + 8000] = true;
    // cout<<d[1]<<endl;
    // cout<<xv[0]<<endl;

    for(int i = 2; i < xv.size() + 2; i++)
    {
        // cout<<xv[i-2]<<endl;
        rep(j, 16000)
        {
            if(dp[i-1][j-xv[i-2]] == true || dp[i-1][j+xv[i-2]] == true)
            {
                dp[i][j] = true;
            }
        }
    }

    // rep(i, xv.size() + 2)
    // {
    //     for(int j = 7995; j < 8020; j++)
    //     {
    //         cout<<dp[i][j]<<" ";
    //     }
    //     cout<<endl;
    // }


    if(dp[xv.size() + 1][x + 8000] != true)
    {
        cout<<"No"<<endl;
        return;
    }

    rep(i, n+1)
    {
        rep(j, 16000) dp[i][j] = false;
    }

    dp[0][8000] = true;
    // dp[1][d[1] + 8000] = true;
    // cout<<d[1]<<endl;
    // cout<<xv[0]<<endl;

    for(int i = 1; i < yv.size() + 1; i++)
    {
        // cout<<xv[i-2]<<endl;
        rep(j, 16000)
        {
            if(dp[i-1][j-yv[i-1]] == true || dp[i-1][j+yv[i-1]] == true)
            {
                dp[i][j] = true;
            }
        }
    }

    // rep(i, yv.size() + 1)
    // {
    //     for(int j = 7995; j < 8020; j++)
    //     {
    //         cout<<dp[i][j]<<" ";
    //     }
    //     cout<<endl;
    // }


    if(dp[yv.size()][y + 8000] != true)
    {
        cout<<"No"<<endl;
        return;
    }

    cout<<"Yes"<<endl;
}

int main()
{   

    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T=1;
    // cin>>T;
    int t=0;
    while(t++<T)
    {
        // cout<<"Case #"<<t<<":"<<' ';
        solve();
        // cout<<'\n';
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}