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 scala.collection.mutable
import scala.io.StdIn._

sealed abstract class Direction
case object North extends Direction
case object East extends Direction
case object South extends Direction
case object West extends Direction

object Main extends App {
  val s = readLine()
  val Array(gx, gy) = readLine().split(" ").map(_.toInt)

  if (search(0, 0, East, s)) println("Yes") else println("No")

  def sides(d: Direction): (Direction, Direction) = d match {
    case North | South => (East, West)
    case _ => (North, South)
  }

  def opposite(d: Direction): Direction = d match {
    case North => South
    case East => West
    case South => North
    case West => East
  }

  def search(x: Int, y: Int, d: Direction, s: String): Boolean = {
    if (s.isEmpty) {
      if (gy == y && gx == x) return true
      else return false
    }

    s.head match {
      case 'T' =>
        var count = 1
        while (count < s.length && s(count) == 'T') {
          count += 1
        }
        if (count % 2 == 1) {
          val next = sides(d)
          search(x, y, next._1, s.tail) ||
            search(x, y, next._2, s.tail)
        } else if (count == 2)
          search(x, y, opposite(d), s.substring(count))
        else {
          search(x, y, d, s.substring(count)) ||
            search(x, y, opposite(d), s.substring(count))
        }

      case _ =>
        d match {
          case North => search(x, y + 1, d, s.tail)
          case East => search(x + 1, y, d, s.tail)
          case South => search(x, y - 1, d, s.tail)
          case West => search(x - 1, y, d, s.tail)
        }
    }
  }

}