problems / 185

Browser History Navigation

easy design simulation stack 1500 ms · 256 MB

Browser History Navigation

You're implementing a simple web browser. It maintains a history of visited URLs and supports three operations:

  • VISIT(url): Navigate to the given URL. This clears any "forward" history (if the user had previously used back, those URLs are no longer reachable).
  • BACK(): Go to the previous URL in the history. If already at the beginning, this does nothing.
  • FORWARD(): Go to the next URL in the history. If already at the end, this does nothing.

Initially, the browser is at the first visited URL. Given a sequence of operations, output the URL at each step.

Input

The first line contains n (1 ≤ n ≤ 10^5), the number of operations.

Each of the next n lines contains one operation:

  • VISIT url where url is a non-empty string of lowercase letters (1 ≤ |url| ≤ 20)
  • BACK
  • FORWARD

The first operation is always a VISIT.

Output

For each VISIT, BACK, and FORWARD operation, print the current URL after that operation.

Constraints

  • 1 ≤ n ≤ 10^5
  • Each URL is 1–20 lowercase letters
  • At least one operation is always a VISIT

Notes

In the first example, after visiting github and stackoverflow, when we go BACK, we're at github. From there, if we VISIT google, the forward history is cleared and we can no longer reach stackoverflow.

Example 1

Input

5
VISIT github
VISIT stackoverflow
BACK
FORWARD
VISIT google

Output

github
stackoverflow
github
stackoverflow
google

Example 2

Input

7
VISIT a
VISIT b
VISIT c
BACK
BACK
FORWARD
FORWARD

Output

a
b
c
b
a
b
c

Hints · reveal one at a time if you're stuck

Hint 1

Think of the history as a list of pages with a pointer to the current one. What does a VISIT do to the pages that lie ahead of the pointer?

Hint 2

Keep two stacks: one for pages behind the current page and one for pages ahead of it. Work out what BACK and FORWARD move between them, and how VISIT differs from both.

report an issue Prefer your own editor? sooks prep 185 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.