Loop Master
Problem Description Maya, a coding enthusiast, is working on a new task today. She will receive a set of commands from her colleague, which specify a for loop or nested for loops. Maya wants to write a program that will read these commands and output the corresponding results.
The commands her colleague provides will consist of the keywords: {for, do, done, break, continue, print}
where, for - defines a for loop
do - starting of a for loop
done - ending of a for loop
break N - exits the current loop when current loop is on the Nth iteration
continue N - skips the rest of the parts of current loop at the Nth iteration
print "statement" - prints the statements inside the quotes.
A simple for and nested for loops look like below.
for 5 times
do
print "hello world!"
done
This set of commands will print "hello world!" five times.
A nested for loop will look like below
for 2 times
do
for 1 time
do
print "hello world!"
done
done
This set of commands will print "hello world!" two times.
Her colleague said that if she solves this, they will crown her the "Loop Master". Assist Maya in becoming a Loop Master by helping her determine the output of the commands received from her colleague.
Assume that all commands are syntactically correct i.e., all loops will end correctly.
Constraints 1 <= number of lines in the command set <= 50
Input First line consists of N, the number of lines in the given set of commands.
Next N lines contain the commands.
Output Process and print the result of the given commands.
Time Limit (secs) 1
Examples Example 1
Input
10
for 5 times
do
continue 3
print "hello"
for 5 times
do
print "world"
break 3
done
done
Output
hello
world
world
world
hello
world
world
world
hello
world
world
world
hello
world
world
world
Explanation
The given commands run an outer loop 5 times and inner loop 5 times. In the outer third iteration, continue 3 skips the rest of the loop, so nothing is printed. In the inner third iteration, break 3 will break the loop. Rest all the time, both the statements {hello, world} will be printed.
Example 2
Input
5
for 4 times
do
print "welcome!"
continue 2
done
Output
welcome!
welcome!
welcome!
welcome!
Explanation
There is only one loop which iterates 4 times and the continue statement is encountered after the execution of the print statement. So, nothing to be skipped.