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: Runtime Error)

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = long long;
constexpr ll INF = (1LL<<60);
constexpr ll MOD = (1e9+7);
template<class T>inline bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>inline bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
inline void dump(){cout<<endl;}
template<class Head,class... Tail>inline void dump(Head&& head, Tail&&... tail){cout<<head<<", ";dump(forward<Tail>(tail)...);}
template<typename T>inline istream &operator>>(istream&input,vector<T>&v){for(auto &elemnt:v)input>>elemnt;return input;}
template<class T>vector<T> make_vector(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto make_vector(size_t a, Ts... ts){return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));}

struct pos{
  int y,x;
  pos(){}
  pos(int _y,int _x):y(_y),x(_x){}
  pos operator+(pos const& a){
    return pos(y+a.y,x+a.x);
  }
  pos operator-(pos const& a){
    int _y=y-a.y;
    int _x=x-a.x;
    return pos(_y,_x);
  }
  pos& operator+=(pos const& a){
    x+=a.x;y+=a.y;
    return (*this);
  }
  pos& operator-=(pos const& a){
    y-=a.y;
    x-=a.x;
    return *this;
  }
  pos& operator=(pos const& a){x=a.x;y=a.y;return (*this);}
  bool operator==(pos const& rhs)const{
    return y==rhs.y&&x==rhs.x;
  }
  bool operator!=(pos const& rhs)const{
    return !(y==rhs.y&&x==rhs.x);
  }
  bool operator<(pos const& rhs)const{
    return y<rhs.y;
  }
  bool operator>(pos const& rhs)const{
    return y>rhs.y;
  }
};
using pll = pair<ll,ll>;
main(){

  string s;
  ll n,y,x;
  cin>>s>>x>>y;
  n = s.size();

  vector<ll> mx,my;
  bool f=true;
  ll cnt=0;
  rep(i,n){
    if(s[i]=='F')cnt++;
    else{
      if(f)mx.emplace_back(cnt);
      else my.emplace_back(cnt);
      cnt=0;
      f = !f;
    }
  }

  vector<bool> xdp(1e5,false);
  vector<bool> ydp(1e5,false);

  xdp[0]=xdp[mx[0]]=true;
  queue<pll> que;
  que.emplace(1,mx[0]);
  while(!que.empty()){
    ll idx,p;
    tie(idx,p) = que.front();
    que.pop();
    if(idx>=mx.size())break;
    ll a,b;
    a = p+mx[idx];
    b = p-mx[idx];
    xdp[a] = xdp[b] = true;
    que.emplace(idx+1,a);
    que.emplace(idx+1,b);
  }

  ydp[0]=true;
  queue<pll> que2;
  que2.emplace(0,0);
  while(!que2.empty()){
    ll idx,p;
    tie(idx,p) = que2.front();
    que2.pop();
    if(idx>=my.size())break;
    ll a,b;
    a = p+my[idx];
    b = p-my[idx];
    xdp[a] = xdp[b] = true;
    que2.emplace(idx+1,a);
    que2.emplace(idx+1,b);
  }

  cout<<(xdp[x]&&ydp[y]?"Yes":"No")<<endl;

}

void __main(){

  string s;
  ll n,y,x;
  cin>>s>>x>>y;
  n = s.size();

  ll move_x,move_y;
  move_x = move_y = 0;
  bool b =true;

  rep(i,n){
    if(s[i]=='F'){
      (b?move_x:move_y)++;
    }else{
      b = !b;
    }
  }

  if( ((move_y&1)==(y&1))&&((move_x&1)==(x&1)) )puts("Yes");
  else puts("No");

}

void _main(){

  string s;
  ll x,y;
  cin>>s>>x>>y;
  ll n = s.size();

  ll ny,nx;
  ny=nx=0;
  bool dir_x = true;

  rep(i,n){
    if(s[i]=='F'){
      if(dir_x){
        if(nx<x)nx++;
        else nx--;
      }else{
        if(ny<y)ny++;
        else ny--;
      }
    }else{
      dir_x = !dir_x;
    }
  }

  if(ny==y&&nx==x)puts("Yes");
  else puts("No");

}