cswork / OA打开练习工作台

AirbnbOA #6

Print Sentences as Table

简单可在线判题

考点(AI 标注)字符串

Given n sentences and a fixed table content width width, print each sentence as one row of a bordered table.

A border line is +, followed by width+2 copies of -, followed by +. A sentence line is | , followed by the sentence itself, padded with spaces so the content takes exactly width characters, followed by |.

For each sentence in order, output a border line and then its sentence line; finish with one more border line. When n=0, output nothing.

输入格式

The first line contains two integers n and width. Each of the next n lines contains one sentence (the whole line is the sentence content; it may contain spaces and may be an empty line).

0≤n≤100000, 1≤width≤300; sentences consist of printable ASCII characters and spaces, with length at most width.

输出格式

When n>0, output a table of 2n+1 lines, each ending with a newline; when n=0, the output is empty. The output is compared character by character, so leading and trailing spaces of sentences must be preserved exactly.

样例 1

输入:

3 55
Hello world
How are you today
Bye

输出:

+---------------------------------------------------------+
| Hello world                                             |
+---------------------------------------------------------+
| How are you today                                       |
+---------------------------------------------------------+
| Bye                                                     |
+---------------------------------------------------------+

样例 2

输入:

0 5

输出:

样例 3

输入:

4 6
  ab

x y z 
|-+|-+

输出:

+--------+
|   ab   |
+--------+
|        |
+--------+
| x y z  |
+--------+
| |-+|-+ |
+--------+

样例说明

Example 1: width=55, so every border is + followed by 57 copies of - and then +; each sentence line pads the sentence with spaces to 55 characters. Example 2: n=0, so there is no output. Example 3: the two leading spaces of the first sentence, the empty second sentence, and the trailing space of the third sentence all count toward the content width as is.