Loading...
Loading...
Arjun opens a new browser tab to check his college portal. The tab starts on the page home.
You get q commands, one per line: - visit page: open page from the current one. Every page you could have gone forward to is forgotten. A visit prints nothing. - back k: go back k pages, or as far as possible if there are fewer than k pages behind you. Print the page you are now on. - forward k: go forward k pages, or as far as possible. Print the page you are now on.
Visiting the page you are already on still counts as a new step.
visit page, back k or forward k.back and forward command: the page you are on after it.back or forward.Example
Input
8 visit notes visit timetable visit results back 2 visit checkout forward 1 back 1 forward 5
Output
notes checkout notes checkout
After back 2 the tab is on notes. Visiting checkout forgets timetable and results, so forward 1 has nowhere to go and stays on checkout.
Try the problem first. Open one hint at a time.
At any moment there are pages behind you and pages ahead of you. A visit changes both groups.
A visit adds one page behind you and throws away every page ahead.
Keep all pages in a list with an index for the current page, and remember where the last valid page is. Back and forward only move the index, so a large k costs nothing.
An ArrayList with a current index and a last index does the job. Two stacks (one for back, one for forward) also work, but then a large k means many pops.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor