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: Wrong Answer)

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define N 100010
char a[N];
int s1[N],s2[N],t1[N],t2[N];
int main()
{
    int i,j,k,x,y,flag,n,m,k1,k2;
    int r1,r2;
    scanf("%s",a);
    scanf("%d%d",&n,&m);
    k=strlen(a);
    flag=0;
    x=0;
    y=0;
    for(i=0;i<k;i++)
    {
        if(a[i]=='T')
        flag=(flag+1)%2;
        else
        if(a[i]=='F')
        {
            if(flag==0)
            x++;
            else
            y++;
        }
    }
    if((x-n)%2==1||(y-m)%2==1)
    {
        printf("No\n");
        return 0;
    }
    i=0;
    while(a[i]=='F'&&i<k)
    {
        n--;
        i++;
    }
    if(i==k)
    {
        if(n==0&&m==0)
        printf("Yes\n");
        else
        printf("No\n");
        return 0;
    }
    flag=0;
    k1=0;
    k2=0;
    x=0;
    y=0;
    for(;i<k;i++)
    {
        if(a[i]=='T')
        {
            if(flag==0)
            {
                if(x>0)
                s1[k1++]=x;
                x=0;
                flag=1;
            }
            else
            if(flag==1)
            {
                if(y>0)
                s2[k2++]=y;
                y=0;
                flag=0;
            }
        }
        else
        if(a[i]=='F')
        {
            if(flag==0)
            x++;
            else
            if(flag==1)
            y++;
        }
    }
    if(flag==0)
    {
        if(x>0)
        s1[k1++]=x;
        x=0;
        flag=1;
    }
    else
    if(flag==1)
    {
        if(y>0)
        s2[k2++]=y;
        y=0;
        flag=0;
    }
    r1=0;
    for(i=0;i<k1;i++)
    r1=r1+s1[i];
    r2=0;
    for(i=0;i<k2;i++)
    r2=r2+s2[i];
    n=fabs(n);
    m=fabs(m);
    if(r1<n||r2<m)
    {
        printf("No\n");
        return 0;
    }
    memset(t1,0,sizeof(t1));
    memset(t2,0,sizeof(t2));
    t1[r1]=1;
    for(i=0;i<k1;i++)
    for(j=r1;j>=s1[i];j--)
    t1[j-s1[i]]=t1[j]||t1[j-s1[i]];
    t2[r2]=1;
    for(i=0;i<k2;i++)
    for(j=r2;j>=s2[i];j--)
    t2[j-s2[i]]=t2[j]||t2[j-s2[i]];
    if(t1[r1-n]&&t2[r2-m])
    printf("Yes\n");
    else
    printf("No\n");
    return 0;
}