cho.sh
Notes
Loading...

Cascading Windows

Time limit

2s

Memory limit

128 MB

Problem

A text-mode screen contains several windows. Each window consists only of +, -, |, ., and a lowercase alphabetic title, and has the following form.

+--|abcdef|---+|.............||.............||.............||.............|+-------------+

Every window follows this form. The width of each window is large enough to display its title. The title is placed exactly in the center of the top border; if the two side margins cannot be equal, the right margin is one character longer than the left margin.

You are given a screenshot. No two windows overlap. Rearrange all windows into a cascading layout according to the following rules and print the resulting screen.

  1. The width and height of every window must stay unchanged.
  2. Sort the windows by title in lexicographic order.
  3. Move the top-left corner of the first sorted window to the top-left corner of the whole screen. Then place each following window one cell to the right and one cell down from the previous position.

Input

The first line contains the screen height M and width N. (10 <= M, N <= 100) The next M lines each contain a string of length N, describing the current screenshot. The background is represented only by . characters. Every window title has length between 1 and 10 inclusive. You may assume that M and N are large enough to place all windows in the cascading layout.

Output

Print M lines showing the screen after all windows have been rearranged into the cascading layout.

+--|abcdef|---+|.............||.............||.............||.............|+-------------+