IOI '12 - Sirmione/Montichiari, Italy
Crayfish Scrivener
Some people say that Leonardo was a great admirer of Johannes Gutenberg, the German blacksmith who invented movable-type printing, and that he paid homage by designing a machine called the crayfish scrivener — il gambero scrivano — a very simple typing device. It is somehow similar to a simple modern typewriter and accepts only two commands: one to type the next character and one to undo the most recent commands. The notable feature of the crayfish scrivener is that the undo command is extremely powerful: an undo is also considered to be a command itself, and can be undone.
Statement
Your task is to realize a software version of the crayfish scrivener: it starts with an empty text and accepts a sequence of commands entered by the user, and queries for specific positions of the current version of the text, as follows.
TypeLetter L
— append to the end of the text a single lowercase letterL
chosen froma
, …,z
.UndoCommands U
— undo the the lastU
commands, for a positive integerU
.GetLetter P
— output the letter at positionP
in the current text, for a non-negative indexP
. The first letter in the text has index 0. (This query is not a command and thus is ignored by the undo command.)
These operations can be called zero or more times in any order. It is guaranteed that U will not exceed the number of previously received commands, and that P will be less than the current text length (the number of letters in the current text).
As for UndoCommands U
, it undoes the previous U
commands in reverse order: if the command to be undone is TypeLetter L
, then it removes L
from the end of the current text; if the command to be undone is UndoCommands X
for some value X
, it re-does the previous X
commands in their original order.
Input Format
Your program must read the input in the following format:
- line 1: the total number of commands and queries in the input;
- on each following line:
-
- T followed by a space and a lowercase letter for a
TypeLetter
command; - U followed by a space and an integer for
UndoCommands
; - P followed by a space and an integer for
GetLetter
.
- T followed by a space and a lowercase letter for a
Example
We show a possible sequence of calls, together with the state of the text after each call.
Input | Call | Output | Current text |
---|---|---|---|
14 |
|
|
|
T a | TypeLetter a |
| a |
T b | TypeLetter b |
| ab |
P 1 | GetLetter 1 | b | ab |
T d | TypeLetter d |
| abd |
U 2 | UndoCommands 2 |
| a |
U 1 | UndoCommands 1 |
| abd |
P 2 | GetLetter 2 | d | abd |
T e | TypeLetter e |
| abde |
U 1 | UndoCommands 1 |
| abd |
U 5 | UndoCommands 5 |
| ab |
T c | TypeLetter c |
| abc |
P 2 | GetLetter 2 | c | abc |
U 2 | UndoCommands 2 |
| abd |
P 2 | GetLetter 2 | d | abd |
Subtask 1 [5% of points]
The total number of commands and queries is between 1 and 100 (inclusive) and there will be no calls to UndoCommands
.
Subtask 2 [7% of points]
The total number of commands and queries is between 1 and 100 (inclusive) and no UndoCommands
will be undone.
Subtask 3 [22% of points]
The total number of commands and queries is between 1 and 5 000 (inclusive).
Subtask 4 [26% of points]
The total number of commands and queries is between 1 and 1 000 000 (inclusive). All calls to GetLetter
will occur after all calls to TypeLetter
and UndoCommands
.
Subtask 5 [40% of points]
The total number of commands and queries is between 1 and 1 000 000 (inclusive).
All Submissions
Best Solutions
Point Value: 20 (partial)
Time Limit: 2.00s
Memory Limit: 256M
Added: Aug 12, 2013
Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3
Comments (Search)
Indeed, this is what has happened in your submission.
Edit:You need to output letters on new lines.
O(log N) for GetLetter
Edit: Java is just slow :(