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<cstdio>
#include<queue>
#include<utility>
#include<cstring>
#include<stack>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<map>
#define MAX_N 100001
#define INF_INT 2147483647
#define INF_LL 9223372036854775807
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;

int main()
{
  int cnt=0,x,y,f=0,nf=0,i=0,ind=0;
  string s;
  vector<int> X(0),Y(0);
  cin >> s;
  cin >> x >> y;
  for(int i=0;i<s.size();i++){
    if(s[i] == 'T'){
      ind = i;
      break;
    }
  }
  for(int j=ind;j<s.size();j++){
    if(s[j] == 'T'){
      if(cnt!=0){
        if(nf)Y.push_back(cnt);else X.push_back(cnt);
      }
      if(nf)nf=0;else nf=1;
      cnt = 0;
      continue;
    }else{
      cnt++;
    }
  }
  if(cnt!=0){
    if(nf)Y.push_back(cnt);else X.push_back(cnt);
  }

  vector<vector<bool> > dpx(X.size()+1,vector<bool>(2*s.size()+1,false));
  vector<vector<bool> > dpy(Y.size()+1,vector<bool>(2*s.size()+1,false));
  dpx[0][s.size()+ind] = true;
  REP(i,X.size()){
    REP(j,2*s.size()+1){
      if(j - X[i] < 0 && j+X[i] > 2*s.size()){
        dpx[i+1][j] = false;
        continue;
      }
      if(j - X[i] >= 0 && j+X[i] <= 2*s.size())
        dpx[i+1][j] = dpx[i][j-X[i]] || dpx[i][j+X[i]];
      else if(j-X[i] < 0)
        dpx[i+1][j] = dpx[i][j+X[i]];
      else
        dpx[i+1][j] = dpx[i][j-X[i]];
    }
  }
  dpy[0][s.size()] = true;
  REP(i,Y.size()){
    REP(j,2*s.size()+1){
      if(j - Y[i] < 0 && j+Y[i] > 2*s.size()){
        dpy[i+1][j] = false;
        continue;
      }
      if(j - Y[i] >= 0 && j+Y[i] <= 2*s.size())
        dpy[i+1][j] = dpy[i][j-Y[i]] || dpy[i][j+Y[i]];
      else if(j-Y[i] < 0)
        dpy[i+1][j] = dpx[i][j+Y[i]];
      else
        dpy[i+1][j] = dpx[i][j-Y[i]];
    }
      
  }
  if(dpx[X.size()][s.size()+x] && dpy[Y.size()][s.size()+y])
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}