cswork / OAOpen practice workspace

UberOA #13

Newspaper Page LEFT/RIGHT Alignment

Estimated EasyJudge-ready

Topics (AI-labelled)StringsSimulation

Each paragraph has a LEFT or RIGHT alignment tag. In the original order, greedily pack as many entries as possible into each line, putting one space between entries and never splitting an entry. The leftover space in a line goes on the right (LEFT) or on the left (RIGHT). Each line of the outer frame is width+2 wide; the top and bottom lines are all *, each content line has one * at each end, and no extra padding is added. Entries containing spaces, such as "Please look" in the original example, are kept as whole entries.

This site's standard input/output is stated explicitly.

Input

The first line contains width P; each paragraph starts with a line align wordCount, followed by wordCount lines, each with one entry. 5≤width≤100, 1≤P≤50. This site uses 1..100 entries per paragraph, each of length 1..width, made of visible ASCII characters and internal spaces, with no leading or trailing spaces.

Output

Output the complete star-bordered text box without a line count; each line has exactly width+2 characters, all spaces are kept, and the last line is followed by a newline.

Example 1

Input:

16 3
LEFT 2
hello
world
RIGHT 3
How
areYou
doing
RIGHT 3
Please look
and align
to right

Output:

******************
*hello world     *
*How areYou doing*
*     Please look*
*       and align*
*        to right*
******************

Example 2

Input:

5 1
LEFT 3
a
b
c

Output:

*******
*a b c*
*******

Example 3

Input:

5 1
RIGHT 2
a b
cc

Output:

*******
*  a b*
*   cc*
*******

Example notes

Example 1: the output has 7 lines of 18 characters each; hello world is padded with 5 spaces on the right, How areYou doing fills exactly 16 columns, and the last three entries each take one line, padded on the left with 5, 7, and 8 spaces respectively. Example 2: a b c exactly fills the 5 content columns, so no padding is needed. Example 3: putting a b and cc together would need 6 columns, so they must go on two lines, padded on the left with 2 and 3 spaces respectively.

Editorial

Loading solution…