Bracelet Making
After spending a long time working for AUCPL Inc., Lily decides to take a break to attend her favorite artist's concert. At the show, it is a tradition for fans to exchange or gift handcrafted bracelets to foster community, connection and lasting memories. With an upcoming concert approaching, Lily, being a devoted fan, decides to prepare several bracelets in advance using her bracelet-making kit.
To create a bracelet, Lily starts with an empty elastic cord. She performs a sequence of actions, where each action is one of the following:
- Insert a bead onto either end of the cord (left or right), or
- Move some beads from one end of the cord to the other, transferring one by one.
You are given the complete sequence of Lily's actions for constructing a single bracelet. Determine the final arrangement of beads on the bracelet after all actions have been performed.
Input
The first line contains an integer (
), the number of Lily's actions.
The next lines each contains an action. These lines will take the form
INSERT LEFT x (),
INSERT RIGHT x (),
MOVE LEFT y () or
MOVE RIGHT y ().
INSERT LEFT x: insert a bead engraved with messageto the left end
INSERT RIGHT x: insert a bead engraved with messageto the right end
MOVE LEFT y: movebeads from the right end to the left end one by one
MOVE RIGHT y: movebeads from the left end to the right end one by one
It is guaranteed that the first action is always an INSERT.
Output
Output the final message on the bracelet, from left to right, as a string.
Example
Input 1
10
INSERT LEFT u
INSERT LEFT a
INSERT LEFT n
INSERT LEFT i
INSERT RIGHT c
INSERT RIGHT p
INSERT RIGHT l
INSERT RIGHT j
INSERT RIGHT o
MOVE LEFT 2
Output 1
joinaucpl
Starting with an empty bracelet cord, after performing the first 4 INSERT LEFT actions, the bracelet is as follow:

After performing the INSERT RIGHT actions:

Finally, we move the 2 rightmost beads (j, o) from right to left. Since we moved one by one, the bead 'o' get inserted before bead 'j', which results in the final bracelet:

Input 2
6
INSERT LEFT a
INSERT LEFT a
INSERT RIGHT b
INSERT LEFT b
MOVE RIGHT 3
MOVE LEFT 1
Output 2
abba
Input 3
2
INSERT LEFT a
MOVE LEFT 20
Since she transfers each bead one by one, there's always an available bead in the cord for her to transfer in the next move.
Output 3
a
Comments