Simulate the address bar of a browser that supports going back and forward through visited pages.
The first line gives q and the starting page home; when the session begins the current page is home and there is no back or forward history. Then q commands follow:
VISIT url — navigate to url. This makes url the current page and discards all forward history (any pages you had gone BACK from and could have gone FORWARD to). Produces no output.BACK n — move backward up to n pages, but never past the earliest page. Print the page you end up on.FORWARD n — move forward up to n pages, but never past the most recently visited page along the current branch. Print the page you end up on.CURRENT — print the current page without moving.Moving BACK/FORWARD when there is nowhere further to go simply leaves you on the same page (and prints it).
Line 1: an integer q and a token home (the starting page).
Each of the next q lines is VISIT url, BACK n, FORWARD n, or CURRENT.
Page tokens (home, url) contain no spaces.
For each BACK, FORWARD, and CURRENT command, print the current page after the command. Print the results in command order, one per line.
Example 1
Input
6 p0 VISIT p1 VISIT p2 BACK 1 VISIT p3 FORWARD 1 CURRENT
Expected
p1 p3 p3
Explanation
Starting on p0, we visit p1 then p2 (timeline p0→p1→p2, current p2). `BACK 1` moves to p1 and prints p1. `VISIT p3` makes p3 current and discards the forward page p2 (timeline p0→p1→p3). `FORWARD 1` has nowhere to go, so it stays on p3 and prints p3. `CURRENT` prints p3.
Example 2
Input
4 home BACK 2 VISIT a VISIT b BACK 1
Expected
home a
Explanation
We start on home with no earlier pages, so `BACK 2` clamps and prints home. We visit a then b (timeline home→a→b). `BACK 1` moves to a and prints a.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →