Folding a Paper Stripe
InterviewTime limit1sMemory limit256 MB
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 cells for a positive integer . The cells are numbered to from left to right. The picture below is a side view of the stripe for , so . 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 folds the stripe is one cell wide and layers thick. Take the stripe with 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 .
Given the folds, report the cells of the folded stripe from top to bottom.
Input
The input has lines, one per fold. Line holds the -th fold as two letters: LU, LD, RU, or RD. The folds are applied in the order given.
, and the stripe has cells.
Output
Print lines. Line holds the number of the cell that lies -th from the top of the folded stripe.