Under construction

Still being built. Things may change

Web Browser 2

Interview

Time limit1sMemory limit512 MB

Summary
Simulate a web browser with backward, forward, access, and compress operations, then print the current page and both history stacks.
Level

Medium4 of 10

Topics
Simulation, Stack, Implementation, Linked list
Solved
No attempts yet

Problem

We use a 'web browser' to access web pages. A web browser has three main functions: Backward, Frontward, and Access.

When a user accesses a website, information about the web page is stored in the computer's cache space. When the backward or frontward function is used, the page information stored in the cache space is loaded. The web browser that Juheon developed has a curious feature called Compress. Compress reduces consecutive pages with the same number in the backward space into one.

Each function works as follows.

  • When backward is executed
  1. Steps 2 and 3 run only when at least one page is stored in the backward space. When there are 0 pages, this operation is ignored.
  2. The web page currently being viewed is stored in the frontward space.
  3. The most recently visited page in the backward space is accessed. That page is then deleted from the backward space.
  • When frontward is executed
  1. Steps 2 and 3 run only when at least one page is stored in the frontward space. When there are 0 pages, this operation is ignored.
  2. The page currently being viewed is stored in the backward space.
  3. The most recently visited page in the frontward space is accessed. That page is then deleted from the frontward space.
  • When a web page is accessed
  1. All pages stored in the frontward space are deleted.
  2. The current page is added to the backward space, and the next page to access is updated to the current page. However, if this is the first time accessing a web page, the current page is not added to the backward space.
  • When compress is executed
  1. In the backward space, if the same page number appears two or more times consecutively, only the most recent one is kept and the rest are deleted.

After all Q operations are finished, find the number of the currently accessed page and the numbers of the pages stored in the backward space and the frontward space.

In the initial state, both the backward space and the frontward space are empty, and no page is being accessed. Also, the same page number can be accessed multiple times, and in that case, even pages with the same number are treated as having different visit orders. In this problem, the computer's cache capacity is assumed to be large enough.

Input

The first line gives N, the number of types of web pages that can be accessed, and Q, the number of operations the user performs. (1 ≤ N, Q ≤ 2,000)

Starting from the second line, Q operations are given. Each operation means the following.

  • B : execute backward.
  • F : execute frontward.
  • A i : access web page i.
  • C : execute compress.

An A (access) operation appears at least once.

Output

Output over 3 lines.

On the first line, output the number of the currently accessed page.

On the second line, output the page numbers in the backward space in order from the most recently visited. If no page is stored, output -1.

On the third line, output the page numbers in the frontward space in order from the most recently visited. If no page is stored, output -1.

Hint

The figure below shows sample input 1.

The numbers inside the blue shapes represent page numbers.

Examples3

  1. Example 1

    Input
    3 11
    B
    F
    A 1
    A 1
    A 2
    A 3
    B
    A 1
    A 1
    A 2
    C
    
    Expected output
    2
    1 2 1
    -1
    
  2. Example 2

    Input
    2 8
    A 1
    A 1
    A 2
    A 2
    A 2
    B
    B
    C
    
    Expected output
    2
    1
    2 2
    
  3. Example 3

    Input
    3 8
    A 1
    A 2
    A 1
    A 2
    C
    B
    A 3
    A 1
    
    Expected output
    1
    3 1 2 1
    -1