Cascading Windows

Time limit2sMemory limit128 MB

Summary
Parse windows with titles from an ASCII screen, sort them by title, and redraw them cascaded diagonally from the top-left corner.
Level

Medium6 of 10

Topics
Simulation, String, Sorting, Implementation
Solved
No attempts yet

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.

Examples3

  1. Example 1

    Input
    12 12
    ............
    .+-|ana|-+..
    .|.......|..
    .|.......|..
    .|.......|..
    .+-------+..
    ..+-|jozo|-+
    ..|........|
    ..|........|
    ..|........|
    ..|........|
    ..+--------+
    
    Expected output
    +-|ana|-+...
    |+-|jozo|-+.
    ||........|.
    ||........|.
    +|........|.
    .|........|.
    .+--------+.
    ............
    ............
    ............
    ............
    ............
    
  2. Example 2

    Input
    15 22
    ...............+-|a|-+
    .+-|winamp|--+.|.....|
    .|...........|.|.....|
    .|...........|.|.....|
    .|...........|.|.....|
    .|...........|.|.....|
    .+-----------+.|.....|
    ...............|.....|
    +----|kit|----+|.....|
    |.............||.....|
    |.............||.....|
    |.............||.....|
    |.............|+-----+
    |.............|.......
    +-------------+.......
    
    Expected output
    +-|a|-+...............
    |+----|kit|----+......
    ||+-|winamp|--+|......
    |||...........||......
    |||...........||......
    |||...........||......
    |||...........||......
    |++-----------++......
    |.....|...............
    |.....|...............
    |.....|...............
    |.....|...............
    +-----+...............
    ......................
    ......................
    
  3. Example 3

    Input
    14 15
    +---|a|---+....
    |.........|....
    |.........|....
    +---------+....
    ...............
    ...............
    +--|ab|---+....
    |.........|....
    |.........|....
    +---------+....
    +--|abc|--+....
    |.........|....
    |.........|....
    +---------+....
    
    Expected output
    +---|a|---+....
    |+--|ab|---+...
    ||+--|abc|--+..
    +||.........|..
    .+|.........|..
    ..+---------+..
    ...............
    ...............
    ...............
    ...............
    ...............
    ...............
    ...............
    ...............