Ancient Commemorative Monolith
Time limit8sMemory limit256 MB
The program parses each bitmap into nested boxes and glyphs and prints the bracketed transliteration with mirror reading resolved.
- Level
Medium7 of 10
- Topics
- Matrix, Recursion, Brute force, Implementation
- Solved
- No attempts yet
Problem
Alice found an old monolith in a forest. The sentence carved on it is written in an old language. A sentence is made of glyphs and of the rectangles that surround them, and some glyphs in a sentence are mirrored horizontally.
Alice transliterates a sentence with ASCII letters. Each glyph of the dictionary gets one lowercase letter, and a rectangle is written as [ and ]. When a sentence contains a mirrored glyph, she reads that sentence from right to left.
A sentence follows this structure.
- A sentence
<seq>is a sequence of zero or more<term>. - A term
<term>is either a glyph or a<box>. A glyph may be mirrored. - A
<box>is a rectangle surrounding a<seq>. The height of a box is larger than any glyph inside it.
The sentence carved on the monolith is a nonempty <seq>. Each term of the sequence fits in a rectangular bounding box, though the bounding boxes of glyphs are not drawn. The bounding boxes of two adjacent terms never overlap.
Let be the transliteration function. A sequence is written either from left to right or from right to left. In both cases is the leftmost term of the sentence, the second term from the left, and so on.
Write for the mirror image of a glyph . A sequence is written from right to left when it holds at least one single glyph term that cannot be read without mirroring, that is, when an integer exists such that is a single glyph , is missing from the glyph dictionary, and is in it. For such a sequence , and otherwise . Every glyph of a sequence is mirrored whenever that sequence holds at least one glyph that cannot be read without mirroring.
If the term is a box enclosing a sequence , then [ ]. If the term is a glyph , then is the letter assigned to , or the letter assigned to when the sequence holding is written from right to left.
Transliterate the sentences carved on the monoliths.
Input
The input holds several datasets. Two zeros separated by one space end the input.
Each dataset has this format.
n m
glyph1
...
glyphn
string1
...
stringm
() is the number of glyphs and () is the number of monoliths. Each has this format.
c h w
b11...b1w
...
bh1...bhw
is the lowercase letter Alice assigned to the glyph. and (, ) are the height and the width of the bitmap of the glyph. The matrix is the bitmap, where a white cell is . and a black cell is *.
Every glyph gets a distinct letter. Every column of a glyph bitmap holds at least one black cell, and the first row and the last row of a glyph bitmap hold at least one black cell. All glyph bitmaps differ from each other, but the mirror image of one bitmap can equal another bitmap, and a bitmap can be symmetric.
Each has this format.
h w
b11...b1w
...
bh1...bhw
and (, ) are the height and the width of the bitmap of the sentence. As in the glyph dictionary, is the bitmap, where a white cell is . and a black cell is *.
The bitmap carries no noise: every black cell belongs to one glyph or to one rectangle. The height of a rectangle is at least 3 and greater than the height of the tallest glyph in the dictionary. The width of a rectangle is at least 3. A box keeps a margin of one white pixel inside its edge.
Glyphs are never stacked vertically. If two rectangles, or a rectangle and a glyph, share a column, then one of them contains the other. Between every two of the bounding boxes of glyphs and the black cells of rectangles there is at least one white cell. At least one cell of the bitmap is black.
Output
For each monolith print the transliterated sentence on one line. After the output for one dataset print # on one line.