Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Transformation

Write a program which performs a sequence of commands to a given string $str$. The command is one of:

print a b: print from the a-th character to the b-th character of $str$

reverse a b: reverse from the a-th character to the b-th character of $str$

replace a b p: replace from the a-th character to the b-th character of $str$ with p

Note that the indices of $str$ start with 0.

Input

In the first line, a string $str$ is given. $str$ consists of lowercase letters. In the second line, the number of commands q is given. In the next q lines, each command is given in the above mentioned format.

Output

For each print command, print a string in a line.

Constraints

$1 \leq $ length of $str \leq 1000$

$1 \leq q \leq 100$

$0 \leq a \leq b < $ length of $str$

for replace command, $b - a + 1 = $ length of $p$

Sample Input 1

abcde
3
replace 1 3 xyz
reverse 0 2
print 1 4

Sample Output 1

xaze

Sample Input 2

xyz
3
print 0 2
replace 0 2 abc
print 0 2

Sample Output 2

xyz
abc

Solution in Scala (judge verdict: Accepted)

import scala.io.StdIn.{readLine, readInt}

sealed trait Order
case class Print(a: Int, b: Int)              extends Order
case class Reverse(a: Int, b: Int)            extends Order
case class Replace(a: Int, b: Int, p: String) extends Order

object Main extends App {
  val str    = readLine()
  val n      = readInt()
  val lines  = Iterator.fill(n)(readLine())
  val orders = lines.map { _.split(" ") }.map {
    case Array("print", a, b)      => Print(a.toInt, b.toInt)
    case Array("reverse", a, b)    => Reverse(a.toInt, b.toInt)
    case Array("replace", a, b, p) => Replace(a.toInt, b.toInt, p)
  }

  val printStrings = {
    val (_, printStrings) = orders.foldLeft((str, Nil: List[String])) { (accum, order) =>
      val (s, printStrings) = accum
      order match {
        case Print(a, b)      =>
          val ps = s.slice(a, b + 1)
          (s, ps :: printStrings)
        case Reverse(a, b)    =>
          val rev = s.slice(a, b + 1).reverse
          (s.patch(a, rev, b - a + 1), printStrings)
        case Replace(a, b, p) =>
          (s.patch(a, p, b - a + 1), printStrings)
      }
    }
    printStrings.reverse
  }

  printStrings.foreach(println)
}