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 5.4.1) (judge verdict: Runtime Error)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h> // int64_t

#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) > (b) ? (b) : (a))

int get_int2(int *a1, int *a2) {
  scanf("%d %d", a1, a2);
  return 0;
}

int get_str(char *str, int size)
{
    char format[100];
    if (size == 0)
        return 0;
    snprintf(format, sizeof(format), "%%%ds", size);
    scanf(format, str);
    return 0;
}

#define STR_MAX 8000

struct point2d {
    int x;
    int y;
};
int main(void) {
    struct point2d goal;
    static char str[STR_MAX];
    get_str(str, STR_MAX);
    int len = strlen(str);
    str[len] = 'T'; // for guard
    get_int2(&goal.x, &goal.y);
    static int arr[STR_MAX+2];
    int aidx = 0;
    int i;
    for(i = 0; i < len+1; i++) {
        char ch = str[i];
        if(ch == 'F') {
            arr[aidx]++;
        } else { // 'T'
            aidx++;
        }
    }
    // if aidx is odd, make it even
    if(aidx%2 == 1) aidx++;
#ifdef DEBUG
    for(i = 0; i < aidx; i++) {
        printf(" %d", arr[i]);
    }
    putchar('\n');
#endif

    // for x
    static char dp[256][2*STR_MAX+1];
    // ([0][len], [1][len]) = (.x=0, .y=0)
    dp[0][len+arr[0]] = 1;
    dp[1][len+arr[1]] = 1;
    dp[1][len-arr[1]] = 1;
    int pos;
    int flag_x = 0;
    int flag_y = 0;
    for(i = 2; i < aidx; i++) {
        int cnt = arr[i];
        unsigned char idx = i;
        for(pos = -len; pos <= len; pos++) {
            int ppos = pos+len;
            if(pos-cnt >= -len) dp[idx][ppos] |= dp[idx-2][ppos-cnt];
            if(pos+cnt <= len) dp[idx][ppos] |= dp[idx-2][ppos+cnt];
        }
    }

    flag_x = dp[aidx-2][len+goal.x]; // even
    flag_y = dp[aidx-1][len+goal.y]; // odd
#ifdef DEBUG
    printf("%d %d\n", flag_x, flag_y);
#endif
    printf("%s\n", (flag_x & flag_y) ? "Yes" : "No");
    return 0;
}