Under construction

Still being built. Things may change

Folding a Paper Stripe

Interview

Time limit1sMemory limit256 MB

Summary
Simulate the K given half-folds of a 2^K-cell stripe and print the cell numbers from top to bottom.
Level

Easy3 of 10

Topics
Simulation, Array
Solved
No attempts yet

Problem

A paper stripe has N=2KN = 2^K cells for a positive integer KK. The cells are numbered 11 to NN from left to right. The picture below is a side view of the stripe for K=3K = 3, so N=8N = 8. It is a side view, not a top view.

You may fold the stripe in half. Mark it at the center, keep one half where it lies, and turn the other half over onto it, either up or down. That gives four folds:

  • LU: the left half turns up, onto the top of the right half.
  • LD: the left half turns down, under the right half.
  • RU: the right half turns up, onto the top of the left half.
  • RD: the right half turns down, under the left half.

The half that moves is turned over. Its cells end up in mirrored left to right order, and its layers end up in the opposite top to bottom order.

After KK folds the stripe is one cell wide and NN layers thick. Take the stripe with K=3K = 3 and fold it three times, first RU, then LD, then LU. Here is the stripe after each fold:

Read from top to bottom, the cells are now 7,2,3,6,5,4,1,87, 2, 3, 6, 5, 4, 1, 8.

Given the folds, report the cells of the folded stripe from top to bottom.

Input

The input has KK lines, one per fold. Line MM holds the MM-th fold as two letters: LU, LD, RU, or RD. The folds are applied in the order given.

1≤K≤101 \le K \le 10, and the stripe has N=2KN = 2^K cells.

Output

Print 2K2^K lines. Line MM holds the number of the cell that lies MM-th from the top of the folded stripe.

Examples2

  1. Example 1

    Input
    RU
    LD
    LU
    
    Expected output
    7
    2
    3
    6
    5
    4
    1
    8
    
  2. Example 2

    Input
    LU
    
    Expected output
    1
    2