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;

char c[8005];
int x,y;
int nx,ny;
int sx=10000,sy=10000;
int F[8005];
int dpx[8005][20005],dpy[8005][20005];
vector<int> X,Y;

int check_x(int x){

  if(nx==0){
    if(sx==x) return 1;
    else return 0;
  }

  if(c[0]=='T') dpx[0][sx-X[0]]=1;
  dpx[0][sx+X[0]]=1;

  for(int j=1; j<nx; j++){
    if(sx<=x){
      for(int i=sx; i<=20000; i++){
        dpx[j][i]=dpx[j-1][i-X[j]]+dpx[j-1][i+X[j]];
      }
    }
    else{
      for(int i=sx; i>=0; i--){
        dpx[j][i]=dpx[j-1][i-X[j]]+dpx[j-1][i+X[j]];
      }
    }
  }

  return dpx[nx-1][x];

}

int check_y(int y){

  if(ny==0){
    if(sy==y) return 1;
    else return 0;
  }

  dpy[0][sy-Y[0]]=1;
  dpy[0][sy+Y[0]]=1;

  for(int j=1; j<ny; j++){
    if(sy<=y){
      for(int i=sy; i<=20000; i++){
        dpy[j][i]=dpy[j-1][i-Y[j]]+dpy[j-1][i+Y[j]];
      }
    }
    else{
      for(int i=sy; i>=0; i--){
        dpy[j][i]=dpy[j-1][i-Y[j]]+dpy[j-1][i+Y[j]];
      }
    }
  }

  return dpy[ny-1][y];

}

int main() {

cin.tie(0);
ios::sync_with_stdio(false);

cin>>c>>x>>y;

string s=c; int n=s.size();

int j=0; 

for(int i=0; i<n; i++){
  if(c[i]=='T'){
    j++;
  }
  if(c[i]=='F'){
    F[j]++;
  }
}

for(int i=0; i<=j; i++){
  if(i%2==0){
    if(F[i]){
      X.push_back(F[i]);
    }
  }else{
    if(F[i]){
      Y.push_back(F[i]);
    }
  }
}

x=x+sx; y=y+sy;

nx=X.size(); ny=Y.size(); 

if(check_x(x)&&check_y(y)) cout<<"Yes"<<endl;
else cout<<"No"<<endl;

//cout<<X[0]<<" "<<X[1]<<" "<<X[2]<<endl;
//cout<<dpx[1][9998]<<endl;

}