| t | import sys | t | import sys |
| text = sys.stdin.readline().rstrip('\n') | | text = sys.stdin.readline().rstrip('\n') |
| words = text.split() | | words = text.split() |
| w_line = sys.stdin.readline().split() | | w_line = sys.stdin.readline().split() |
| W = int(w_line[0]) | | W = int(w_line[0]) |
| v_w = w_line[1] | | v_w = w_line[1] |
| h_w = w_line[2] | | h_w = w_line[2] |
| inside = W - 2 | | inside = W - 2 |
| | | |
| def key(v, h): | | def key(v, h): |
| return ('L' if v == 'LIGHT' else 'H') + ('L' if h == 'LIGHT' else 'H | | return ('L' if v == 'LIGHT' else 'H') + ('L' if h == 'LIGHT' else 'H |
| ') | | ') |
| corners = {'LL': {'tl': '┌', 'tr': '┐', 'bl': '└', 'br': '┘'}, 'LH': {'t | | corners = {'LL': {'tl': '┌', 'tr': '┐', 'bl': '└', 'br': '┘'}, 'LH': {'t |
| l': '┍', 'tr': '┑', 'bl': '┕', 'br': '┙'}, 'HL': {'tl': '┎', 'tr': '┒', | | l': '┍', 'tr': '┑', 'bl': '┕', 'br': '┙'}, 'HL': {'tl': '┎', 'tr': '┒', |
| 'bl': '┖', 'br': '┚'}, 'HH': {'tl': '┏', 'tr': '┓', 'bl': '┗', 'br': '┛' | | 'bl': '┖', 'br': '┚'}, 'HH': {'tl': '┏', 'tr': '┓', 'bl': '┗', 'br': '┛' |
| }} | | }} |
| tees_right = {'LL': '├', 'LH': '┝', 'HL': '┠', 'HH': '┣'} | | tees_right = {'LL': '├', 'LH': '┝', 'HL': '┠', 'HH': '┣'} |
| tees_left = {'LL': '┤', 'LH': '┥', 'HL': '┨', 'HH': '┫'} | | tees_left = {'LL': '┤', 'LH': '┥', 'HL': '┨', 'HH': '┫'} |
| tees_down = {'LL': '┬', 'LH': '┯', 'HL': '┰', 'HH': '┳'} | | tees_down = {'LL': '┬', 'LH': '┯', 'HL': '┰', 'HH': '┳'} |
| tees_up = {'LL': '┴', 'LH': '┷', 'HL': '┸', 'HH': '┻'} | | tees_up = {'LL': '┴', 'LH': '┷', 'HL': '┸', 'HH': '┻'} |
| crosses = {'LL': '┼', 'LH': '┿', 'HL': '╂', 'HH': '╋'} | | crosses = {'LL': '┼', 'LH': '┿', 'HL': '╂', 'HH': '╋'} |
| hors = {'LIGHT': '─', 'HEAVY': '━'} | | hors = {'LIGHT': '─', 'HEAVY': '━'} |
| vers = {'LIGHT': '│', 'HEAVY': '┃'} | | vers = {'LIGHT': '│', 'HEAVY': '┃'} |
| | | |
| def box_char(up, down, left, right): | | def box_char(up, down, left, right): |
| k = key(v_w, h_w) | | k = key(v_w, h_w) |
| if up and down and left and right: | | if up and down and left and right: |
| return crosses[k] | | return crosses[k] |
| if up and down and right and (not left): | | if up and down and right and (not left): |
| return tees_right[k] | | return tees_right[k] |
| if up and down and left and (not right): | | if up and down and left and (not right): |
| return tees_left[k] | | return tees_left[k] |
| if left and right and down and (not up): | | if left and right and down and (not up): |
| return tees_down[k] | | return tees_down[k] |
| if left and right and up and (not down): | | if left and right and up and (not down): |
| return tees_up[k] | | return tees_up[k] |
| if down and right and (not up) and (not left): | | if down and right and (not up) and (not left): |
| return corners[k]['tl'] | | return corners[k]['tl'] |
| if down and left and (not up) and (not right): | | if down and left and (not up) and (not right): |
| return corners[k]['tr'] | | return corners[k]['tr'] |
| if up and right and (not down) and (not left): | | if up and right and (not down) and (not left): |
| return corners[k]['bl'] | | return corners[k]['bl'] |
| if up and left and (not down) and (not right): | | if up and left and (not down) and (not right): |
| return corners[k]['br'] | | return corners[k]['br'] |
| if left and right and (not up) and (not down): | | if left and right and (not up) and (not down): |
| return hors[h_w] | | return hors[h_w] |
| if up and down and (not left) and (not right): | | if up and down and (not left) and (not right): |
| return vers[v_w] | | return vers[v_w] |
| return ' ' | | return ' ' |
| rows = [] | | rows = [] |
| cur = [] | | cur = [] |
| cur_len = 0 | | cur_len = 0 |
| for w in words: | | for w in words: |
| l = len(w) | | l = len(w) |
| if not cur: | | if not cur: |
| cur = [w] | | cur = [w] |
| cur_len = l | | cur_len = l |
| elif cur_len + 1 + l <= inside: | | elif cur_len + 1 + l <= inside: |
| cur.append(w) | | cur.append(w) |
| cur_len += 1 + l | | cur_len += 1 + l |
| else: | | else: |
| rows.append(cur) | | rows.append(cur) |
| cur = [w] | | cur = [w] |
| cur_len = l | | cur_len = l |
| if cur: | | if cur: |
| rows.append(cur) | | rows.append(cur) |
| row_data = [] | | row_data = [] |
| for row in rows: | | for row in rows: |
| widths = [len(w) for w in row] | | widths = [len(w) for w in row] |
| k = len(row) | | k = len(row) |
| pad = inside - (sum(widths) + (k - 1)) | | pad = inside - (sum(widths) + (k - 1)) |
| widths[-1] += pad | | widths[-1] += pad |
| boundaries = [0] | | boundaries = [0] |
| x = 1 | | x = 1 |
| for i in range(k - 1): | | for i in range(k - 1): |
| x += widths[i] | | x += widths[i] |
| boundaries.append(x) | | boundaries.append(x) |
| x += 1 | | x += 1 |
| x += widths[-1] | | x += widths[-1] |
| boundaries.append(x) | | boundaries.append(x) |
| parts = [] | | parts = [] |
| for i, w in enumerate(row): | | for i, w in enumerate(row): |
| parts.append(w.ljust(widths[i])) | | parts.append(w.ljust(widths[i])) |
| if i < k - 1: | | if i < k - 1: |
| parts.append(vers[v_w]) | | parts.append(vers[v_w]) |
| inner = ''.join(parts) | | inner = ''.join(parts) |
| text_line = vers[v_w] + inner + vers[v_w] | | text_line = vers[v_w] + inner + vers[v_w] |
| row_data.append((boundaries, text_line)) | | row_data.append((boundaries, text_line)) |
| | | |
| def make_hline(up_cols, down_cols): | | def make_hline(up_cols, down_cols): |
| up_cols = set(up_cols) | | up_cols = set(up_cols) |
| down_cols = set(down_cols) | | down_cols = set(down_cols) |
| s = [] | | s = [] |
| for x in range(W): | | for x in range(W): |
| up = x in up_cols | | up = x in up_cols |
| down = x in down_cols | | down = x in down_cols |
| left = x > 0 | | left = x > 0 |
| right = x < W - 1 | | right = x < W - 1 |
| s.append(box_char(up, down, left, right)) | | s.append(box_char(up, down, left, right)) |
| return ''.join(s) | | return ''.join(s) |
| out = [] | | out = [] |
| out.append(make_hline([], row_data[0][0])) | | out.append(make_hline([], row_data[0][0])) |
| out.append(row_data[0][1]) | | out.append(row_data[0][1]) |
| for i in range(1, len(row_data)): | | for i in range(1, len(row_data)): |
| out.append(make_hline(row_data[i - 1][0], row_data[i][0])) | | out.append(make_hline(row_data[i - 1][0], row_data[i][0])) |
| out.append(row_data[i][1]) | | out.append(row_data[i][1]) |
| out.append(make_hline(row_data[-1][0], [])) | | out.append(make_hline(row_data[-1][0], [])) |
| sys.stdout.write('\n'.join(out)) | | sys.stdout.write('\n'.join(out)) |