시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
2 초 128 MB 242 64 57 29.688%

문제

text-mode windows의 개발자인 동혁이는 "cascade mode (계단형 배치)" 부분을 코딩하다가 고심에 빠졌다. text-mode windows에서 하나의 window는 다음과 같이 ‘+’, ‘-’, ‘|’, ‘.’, 그리고 알파벳 소문자로 이루어져 있다.

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

모든 window는 위와 같은 형태를 지키고 있으며, title을 기록할 수 있을 정도로 window는 넓다. (즉, title의 길이보다 모든 window는 적어도 6 이상은 가로로 길다. +-|, |-+가 들어가야 하므로) title은 가로로 정확히 가운데에 표시되어 있으며, 만일 위의 경우와 같이 window의 길이가 홀수인 경우 오른쪽이 왼쪽보다 하나 더 길다.

text-mode windows의 screen-shot이 주어졌다. 어떤 두 개의 window도 서로 겹치지 않게 배치되어 있다고 하자. 이 때, 동혁이를 도와 다음과 같이 "cascade mode"로 window를 정리해 주는 프로그램을 작성하시오.

  1. 모든 window의 가로, 세로 크기는 변하지 않아야 한다.
  2. 모든 window들은 title의 알파벳 순서로 정렬한다.
  3. 모든 window들을 왼쪽 위 꼭지점이 화면 전체의 왼쪽 위 꼭지점이 되도록 옮긴다. 그리고 나서, 순서대로 한 칸씩 오른쪽 아래로 내려 배치한다.

입력

첫째 줄에 스크린의 세로 길이 M과 가로 길이 N이 주어진다. (10≤M, N≤100) 이어서 M개의 줄에는 각각 N개의 문자열이 주어지는데, 이는 screen-shot을 나타내 주는 정보이다. 배경 그림 같은 것은 없고 모두 ‘.’으로 표시되며, 모든 window의 title의 길이는 1 이상 10 이하이다. M과 N은 "cascade mode"로 window를 모두 정리할 수 있을 만큼 충분히 크다고 가정해도 좋다.

출력

M개의 줄에 걸쳐 "cascade mode"로 window를 모두 정리한 화면을 출력한다.

예제 입력 1

15 22
...............+-|a|-+
.+-|winamp|--+.|.....|
.|...........|.|.....|
.|...........|.|.....|
.|...........|.|.....|
.|...........|.|.....|
.+-----------+.|.....|
...............|.....|
+----|kit|----+|.....|
|.............||.....|
|.............||.....|
|.............||.....|
|.............|+-----+
|.............|.......
+-------------+.......

예제 출력 1

+-|a|-+...............
|+----|kit|----+......
||+-|winamp|--+|......
|||...........||......
|||...........||......
|||...........||......
|||...........||......
|++-----------++......
|.....|...............
|.....|...............
|.....|...............
|.....|...............
+-----+...............
......................
......................

힌트

W3sicHJvYmxlbV9pZCI6IjIwNDQiLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJ3aW5kb3dzIiwiZGVzY3JpcHRpb24iOiI8cD50ZXh0LW1vZGUgd2luZG93c1x1Yzc1OCBcdWFjMWNcdWJjMWNcdWM3OTBcdWM3NzggXHViM2Q5XHVkNjAxXHVjNzc0XHViMjk0ICZxdW90O2Nhc2NhZGUgbW9kZSAoXHVhY2M0XHViMmU4XHVkNjE1IFx1YmMzMFx1Y2U1OCkmcXVvdDsgXHViZDgwXHViZDg0XHVjNzQ0IFx1Y2Y1NFx1YjUyOVx1ZDU1OFx1YjJlNFx1YWMwMCBcdWFjZTBcdWMyZWNcdWM1ZDAgXHViZTYwXHVjODRjXHViMmU0LiB0ZXh0LW1vZGUgd2luZG93c1x1YzVkMFx1YzExYyBcdWQ1NThcdWIwOThcdWM3NTggd2luZG93XHViMjk0IFx1YjJlNFx1Yzc0Y1x1YWNmYyBcdWFjMTlcdWM3NzQgJmxzcXVvOysmcnNxdW87LCAmbHNxdW87LSZyc3F1bzssICZsc3F1bzt8JnJzcXVvOywgJmxzcXVvOy4mcnNxdW87LCBcdWFkZjhcdWI5YWNcdWFjZTAgXHVjNTRjXHVkMzBjXHViY2IzIFx1YzE4Y1x1YmIzOFx1Yzc5MFx1Yjg1YyBcdWM3NzRcdWI4ZThcdWM1YjRcdWM4MzggXHVjNzg4XHViMmU0LjxcL3A+XHJcblxyXG48cHJlPlxyXG4rLS18YWJjZGVmfC0tLStcclxufC4uLi4uLi4uLi4uLi58XHJcbnwuLi4uLi4uLi4uLi4ufFxyXG58Li4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