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 Scala (2.11.7) (judge verdict: Wrong Answer)

import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val s = in.next()
  val _x, y = in.next().toInt
  val N = s.length
  var x = _x

  var dir = 0
  var sayuu: List[Int] = Nil
  var updown: List[Int] = Nil

  def calc(com: Char, num: Int): Unit = {
    com match {
      case 'F' =>
        if(dir == 0)
          sayuu = num :: sayuu
        else
          updown = num :: updown
      case 'T' =>
        if(num % 2 == 1)
          dir += 90
        dir %= 180

    }
  }

  var ind = 0
  ////
  breakable {
    while(ind < N) {
      if(s(ind) == 'F') {
        ind += 1
      }
      else
        break
    }
  }

  x -= ind

  while(ind < N) {
    val now = s(ind)
    var r = ind + 1

    breakable {
      while(true) {
        if(r == N || now != s(r)) {
          calc(now, r-ind)
          ind = r
          break
        }
        else {
          r += 1
        }
      }
    }
  }

  // val dplr = new Array[Int](16002)
  // val dpud = new Array[Int](16002)

  var setlr: Set[Int] = Set()
  
  for(e <- sayuu) {
    var now = Set(e, -e)

    for(e2 <- setlr) {
      now = now + (e2 + e) + (e2 - e)
    }

    setlr = now
  }

  var setud: Set[Int] = Set()
  for(e <- updown) {
    var now = Set(e, -e)

    for(e2 <- setud) {
      now = now + (e2 + e) + (e2 - e)
    }

    setud = now
  }

  if(setlr.contains(x) && setud.contains(y))
    println("Yes")
  else
    println("No")
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}