#P10058. Reverse and Rotate

Reverse and Rotate

Problem Description

Given a string SS and nn operations, each operation is one of the following three types:

  1. < x means cyclically shift SS to the left by xx positions. For example, abcde\mathtt{abcde} becomes cdeab\mathtt{cdeab} after < 2.

  2. > x means cyclically shift SS to the right by xx positions. For example, abcde\mathtt{abcde} becomes deabc\mathtt{deabc} after > 2.

  3. rev means reverse SS. For example, abcde\mathtt{abcde} becomes edcba\mathtt{edcba} after rev.

Find the string SS' obtained after performing these nn operations in order.

Note: For S=s0s1sk1S=s_0s_1\cdots s_{k-1}, cyclically shifting it left by xx positions becomes sxSx+1Sx+k1s_{-x}S_{-x+1}\cdots S_{-x+k-1}; cyclically shifting it right by xx positions becomes sxSx+1Sx+k1s_{x}S_{x+1}\cdots S_{x+k-1}. For xy(modk)x\equiv y\pmod k, we have sx=sys_x=s_y.

Input Format

The first line contains a string SS.

The second line contains an integer nn.

The next nn lines each contain one operation.

Output Format

Output one line containing a string SS', which is the string obtained after performing the nn operations in order.

abcde
3
> 2
rev
< 2
aedcb

Hint

[Sample 1 Explanation]

  • The original string is abcde\mathtt{abcde}.
  • After the first operation, the string becomes deabc\mathtt{deabc}.
  • After the second operation, the string becomes cbaed\mathtt{cbaed}.
  • After the third operation, the string becomes aedcb\mathtt{aedcb}.

[Constraints]

Let S|S| denote the length of string SS.

Test Point ID Special Property
131\sim3 S,n,x1000\vert S\vert,n,x \le 1000
44 There is no rev operation.
55 S=aa...abb...bS=\mathtt{aa...abb...b}
6106\sim10 None.

For 100%100\% of the testdata, 1S,n1061 \le |S|,n \le 10^6, 0x1090 \le x \le 10^9, and SS consists only of lowercase letters.

Translated by ChatGPT 5