Apply a Cold Compress
InterviewTime limit1sMemory limit128 MB
Decode each compression-expression into its smallest pixel picture by parsing split tags and computing the relative scaling of the two halves, then draw the framed grid.
- Level
Medium7 of 10
- Topics
- Recursion, Divide and conquer, Implementation, Math
- Solved
- No attempts yet
Problem
Many digital-image compression techniques work by finding and describing regions of a single uniform color. Here is a simple scheme for compressing black-and-white images (it extends easily to color). The idea is to repeatedly split the picture in half — either vertically or horizontally — until every sub-picture contains just one color.
A rectangular image is described by a compression-expression, defined as follows. Every compression-expression begins with a two-bit tag, which may be followed by more compression-expressions depending on the tag:
- 00 — a square region of entirely black pixels. Depending on context this square may be a single pixel, a square, a square, and so on.
- 11 — a square region of entirely white pixels, again of any size depending on context.
- 10 — a horizontal split, followed by two compression-expressions. The picture is formed by placing the first picture on the left and the second on the right. A horizontal split is only possible between two pictures of the same height.
- 01 — a vertical split, followed by two compression-expressions. The picture is formed by placing the first picture on top and the second underneath. A vertical split is only possible between two pictures of the same width.
When interpreting a split, the two components may need to be rescaled so that their shared dimension matches. For example, given a picture (2 wide, 6 tall) and a picture :
- A vertical split is possible only if we scale by 3 (to ) and by 2 (to ); the combined picture is then .
- A horizontal split is possible only if we scale by 2 (to ) and by 3 (to ); the combined picture is then .
Using X for a black pixel and a space for a white pixel, the expression 00 denotes
---
|X|
---
and the expression 1000010011 denotes
-----
|XXX|
|XX |
-----
For any compression-expression there is a unique smallest picture it can denote; the same expression can also denote pictures twice that size, three times that size, and so on.
Input
Each line of input contains one compression-expression: a single line of an arbitrary number of 0s and 1s. Input continues until end of file.
Every expression in the input is a valid compression-expression.
Output
For each input expression, print the smallest black-and-white picture it denotes, drawing black pixels as X and white pixels as spaces, and framing it with - and | characters exactly as in the pictures above. Print nothing — no characters and no extra whitespace — outside the frame, apart from the newline that ends each line.
Your output must contain no blank lines.