kaskade | 프로그래밍의 벗 PivotOJ
PivotOJ

kaskade

시간 제한: 2000ms메모리 제한: 128MB출처: CHC 2006 Regional Competition - JuniorsBOJ 2044

문제

A number of windows are displayed on the text-mode screen. 

One window consists of the border represented by the characters '-' (minus), '|' (vertical line) and '+' (plus), of the interior represented by the characters '.' (dot), and the title of the window that is located in the middle of the upper border. The title is centered or a little to the left if exact centering is impossible. More precisely, the distance between the first letter of the title and left border will be equal to or one less than the distance between the last letter and the right border. 

Each window is wide enough to contain the title, together with the adjacent strings '-|' on the left, and '|-' on the right (more precisely, the width of the window is at least 6 columns greater than the length of the title). The height of each window is at least 3 rows, and no two different windows have the same title.

+--|window|---+
|.............|
|.............|
|.............|
|.............|
+-------------+

We are given the layout of a screen with a number of windows on it such that no two windows overlap. 

We have to arrange the windows in the so called "cascade mode" defined as follows: 

  • the height and width of the windows must not be changed 
  • the upper left corner of the first window has to be in the upper left corner of the screen 
  • each next window should overlap with the previous one and its upper left corner has to be shifted exactly one row down and one column right
  • windows should be alphabetically sorted by title – the first window in the first row, the second window in the second row etc. 

입력

The first line of input contains two integers M and N, 10 ≤ M,N ≤ 100, the numbers of rows and columns of the screen. 

Each of the following M lines contains N characters representing the layout on the screen. 

The length of the title of each window will be at least 1 and at most 10 characters. The only characters that are allowed are lowercase letters of the English alphabet ('a'-'z'). The numbers M and N will be large enough that all of the windows, after the rearranging, completely fit onto the screen.

출력

The output should contain M lines with N characters each – the layout of the screen after the windows are rearranged. 

예제

예제 1

입력
12 12
............
.+-|ana|-+..
.|.......|..
.|.......|..
.|.......|..
.+-------+..
..+-|jozo|-+
..|........|
..|........|
..|........|
..|........|
..+--------+
출력
+-|ana|-+...
|+-|jozo|-+.
||........|.
||........|.
+|........|.
.|........|.
.+--------+.
............
............
............
............
............

예제 2

입력
15 22
...............+-|a|-+
.+-|winamp|--+.|.....|
.|...........|.|.....|
.|...........|.|.....|
.|...........|.|.....|
.|...........|.|.....|
.+-----------+.|.....|
...............|.....|
+----|kit|----+|.....|
|.............||.....|
|.............||.....|
|.............||.....|
|.............|+-----+
|.............|.......
+-------------+.......
출력
+-|a|-+...............
|+----|kit|----+......
||+-|winamp|--+|......
|||...........||......
|||...........||......
|||...........||......
|||...........||......
|++-----------++......
|.....|...............
|.....|...............
|.....|...............
|.....|...............
+-----+...............
......................
......................

예제 3

입력
14 15
+---|a|---+....
|.........|....
|.........|....
+---------+....
...............
...............
+--|ab|---+....
|.........|....
|.........|....
+---------+....
+--|abc|--+....
|.........|....
|.........|....
+---------+....
출력
+---|a|---+....
|+--|ab|---+...
||+--|abc|--+..
+||.........|..
.+|.........|..
..+---------+..
...............
...............
...............
...............
...............
...............
...............
...............
코드를 제출하려면 로그인하세요.