Library 2
InterviewTime limit2sMemory limit1024 MB
Process a sequence of push and pop actions on a stack of book titles, printing the title each time a READ pop occurs.
- Level
Easy2 of 10
- Topics
- Stack, Simulation, Implementation
- Solved
- No attempts yet
Problem
Bitaro, who loves reading, decided to borrow books from the library. His house is small, so the floor has room for only 1 book. The ceiling is high enough, however, so he decided to stack books in this space to keep track of them.
Bitaro will now perform Q actions. The i-th action (1 ≤ i ≤ Q) is represented by a string Si. Si is either a string of lowercase English letters or READ, with the following meanings.
- If it is a string of lowercase English letters, Bitaro borrows the book titled
Sifrom the library and places it on top of the stack. - If it is
READ, Bitaro reads the book on top of the stack and returns it to the library.
You want to determine which books Bitaro read and in what order.
Given the contents of the Q actions, write a program that outputs the titles of the books Bitaro read in the order he read them.
Input
The input is given from standard input in the following format.
Q
S1
S2
:
SQ
Output
For each action where Si is READ, output the title of the book Bitaro read, in order, separated by newlines to standard output.
Constraints
2 ≤ Q ≤ 200 000.Qis an integer.Siis a string of length at least1and at most10(1 ≤ i ≤ Q).Siis a string of lowercase English letters orREAD(1 ≤ i ≤ Q).- There is at least one
i(1 ≤ i ≤ Q) for whichSiisREAD. - When
SiisREAD, the stack always contains at least1book (1 ≤ i ≤ Q).