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 JAVA (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main (String[] args) throws IOException
	{
		BufferedReader input = new BufferedReader (new InputStreamReader (System.in));

		char[] str = input.readLine().toCharArray();

		int order = Integer.parseInt(input.readLine());

		//命令をorder回実行する
		for(int kai = 0; kai < order; kai++)
		{
			String[] orderStr = input.readLine().split(" ");

			int start = Integer.parseInt(orderStr[1]);

			int end = Integer.parseInt(orderStr[2]);

			//命令がprintのとき
			if(orderStr[0].equals("print"))
			{
				//startからendまでの文字列を表示する
				for(int num = start; num <= end; num++)
					System.out.print(str[num]);

				System.out.println();
			}

			//命令がreverseのとき
			else if(orderStr[0].equals("reverse"))
			{

				char[] reverse = new char[end - start +1];

				//reverseに指定された範囲の文字列を逆順に代入する
				for(int num = 0; num < end - start + 1; num++)
					reverse[num] = str[end - num];

				//指定範囲を逆順にした文字列を代入する
				for(int num = 0; num < end - start + 1; num++)
					str[start + num] = reverse[num];
			}

			//命令がreplaceのとき
			else if(orderStr[0].equals("replace"))
			{
				char[] replace = orderStr[3].toCharArray();

				//指定範囲に入力された文字列を代入する
				for(int num = 0; num <end - start + 1; num++)
					str[start + num] = replace[num];
			}
		}
	}
}