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

#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

pub trait InputValue {
    fn parse(s: &str) -> Self;
}

pub fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

pub fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

pub fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);
parse_tuple!(A, B, C, D);
parse_tuple!(A, B, C, D, E);

// ===

fn main() {
    let s: String = read();
    let s: Vec<char> = s.chars().collect();
    let (x, y): (i32, i32) = read();
    let mut x = x;

    let n = s.len();
    let mut seq = vec![vec![]; 2];
    let mut dir = 0;
    let mut go = 0;
    let mut first = 0;
    for i in 0..n+1 {
        if i < n && s[i] == 'F' {
            go += 1;
        } else {
            if go >= 1 {
                if first == 1 {
                    x -= go;
                } else {
                    seq[dir].push(go);
                }
            }
            go = 0;
            dir ^= 1;
            first = 0;
        }
    }

    if can_make(&seq[0], x) && can_make(&seq[1], y) {
        println!("Yes");
    } else {
        println!("No");
    }
}

fn can_make(x: &Vec<i32>, t: i32) -> bool {
    let mut m = 10;
    for xi in x {
        m += (*xi) * 2;
    }
    let GETA = m / 2;
    let m: usize = m as usize;
    let mut dp = vec![vec![false; m]; 2];
    let n = x.len();

    dp[0][GETA as usize] = true;
    for i in 0..n {
        let fr = i % 2;
        let to = 1 - fr;
        for j in 0..m {
            dp[to][j] = false;
        }
        let go = x[i] as usize;
        for j in 0..m {
            if dp[fr][j] {
                dp[to][j+go] = true;
                dp[to][j-go] = true;
            }
        }
    }
    dp[n%2][(GETA+t) as usize]
}