All problems

Problem 892

Design Browser History

Medium · 20 pts · Design, Stack, Array

Problem

A browser starts on HOMEPAGE. 'visit url' opens url from the current page and erases all forward history. 'back k' moves back up to k steps (stopping at the first page), and 'forward k' moves forward up to k steps (stopping at the last page); after each back or forward, print the current page. Keep the history in an array with a current index and a 'last valid index' that visit resets.

Input

The first line contains HOMEPAGE and Q. Each of the next Q lines contains one operation. There is at least one back or forward.

Output

Print the current page after every back and forward, one per line.

Constraints

1 <= Q <= 5000; 1 <= k <= 100; page names are lowercase words

Example input

leetcode 9
visit google
visit facebook
visit youtube
back 1
back 1
forward 1
visit linkedin
forward 2
back 2

Example output

facebook
google
facebook
linkedin
google

Explanation

From leetcode, visiting google, facebook and youtube, then back 1 shows facebook and back 1 again shows google. forward 1 shows facebook; visiting linkedin erases youtube, so forward 2 stays on linkedin.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Convert Binary Number in a Linked List to Integer Next: Design Front Middle Back Queue