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: Accepted)

use std::io::{BufRead, Write};
use std::io;

fn solve<R: BufRead, W: Write>(mut input: R, mut output: W) {
    let mut buf = String::new();
    input.read_line(&mut buf).unwrap();
    let s = buf.trim_right().to_owned();
    buf.clear();
    input.read_line(&mut buf).unwrap();
    let (x, y): (isize, isize) = {
        let mut split = buf.split_whitespace();
        (split.next().unwrap().parse().unwrap(), split.next().unwrap().parse().unwrap())
    };
    let cnts: Vec<usize> = s.split('T').map(|x| x.len()).collect();
    let x_cnts: Vec<usize> = cnts.iter().cloned().enumerate().filter_map(|(idx, x)| if idx % 2 == 0 { Some(x) } else { None }).skip(1).collect();
    let y_cnts: Vec<usize> = cnts.iter().cloned().enumerate().filter_map(|(idx, x)| if idx % 2 == 1 { Some(x) } else { None }).collect();
    let x_sum: usize = x_cnts.iter().sum();
    let y_sum: usize = y_cnts.iter().sum();
    let x_target = x_sum as isize + x - cnts[0] as isize;
    let y_target = y_sum as isize + y;
    let result = if x_target % 2 == 0 && y_target % 2 == 0 && x_target >= 0 && y_target >= 0 {
        let x_target = x_target as usize / 2;
        let y_target = y_target as usize / 2;
        let mut flag = true;
        for (target, cnts) in [(x_target, x_cnts), (y_target, y_cnts)].iter().cloned() {
            let mut v = vec![vec![false; target + 1]; cnts.len() + 1];
            v[0][0] = true;
            for i in 1..cnts.len() + 1 {
                for w in 0..target + 1 {
                    v[i][w] |= v[i - 1][w];
                    if w >= cnts[i - 1] { v[i][w] |= v[i - 1][w - cnts[i - 1]]; }
                }
            }
            if !v[cnts.len()][target] {
                flag = false;
                break;
            }
        }
        flag
    } else {
        false
    };
    writeln!(output, "{}", if result { "Yes" } else { "No" }).unwrap();
}

fn main() {
    let stdin = io::stdin();
    let stdout = io::stdout();
    solve(stdin.lock(), stdout.lock());
}

#[cfg(test)]
mod tests {
    fn test(input: &str, output: &str) {
        let mut v: Vec<u8> = Vec::new();
        ::solve(input.as_bytes(), &mut v);
        assert_eq!(String::from_utf8(v).unwrap(), output);
    }

    #[test]
    fn case1() {
        test(
            "FTFFTFFF
4 2
",
            "Yes\n",
        );
    }

    #[test]
    fn case2() {
        test(
            "FTFFTFFF
-2 -2
",
            "Yes\n",
        );
    }

    #[test]
    fn case3() {
        test(
            "FF
1 0
",
            "No\n",
        );
    }

    #[test]
    fn case4() {
        test(
            "TF
1 0
",
            "No\n",
        );
    }

    #[test]
    fn case5() {
        test(
            "FFTTFF
0 0
",
            "Yes\n",
        );
    }

    #[test]
    fn case6() {
        test(
            "TTTT
1 0
",
            "No\n",
        );
    }

    #[test]
    fn bonus() {
        test(
            "FFF
-3 0
",
            "No\n",
        );
    }
}