AIOC Banner

Problem: Word Wrap

Want to try solving this problem? You can submit your code online if you log in or register.


Word Wrap

Input File: wrapin.txt
Output File: wrapout.txt
Time Limit: 1 second

KDE, MacOS, Windows, it's all too much. These days everybody has a graphical user interface with a fancy word processor and proportional fonts. Plain text-based programs just aren't as trendy as they used to be.

You are determined to reverse this trend. One day you will make people understand the value of having a word processor that can run on a computer with under a megabyte of memory. Of course, you happen to be writing such a word processor at this very moment.

Your task here is to implement word wrapping in this word processor. That is, given a paragraph of text, you must arrange the words into lines so that the paragraph fits on your screen and is pleasing to the eye.

Since your word processor is a plain text-based program, you are using fixed width fonts. If your screen has width w, this means that each line of your screen can fit precisely w characters, including letters, digits, punctuation and spaces.

A word is defined to be a consecutive sequence of letters, digits and/or punctuation surrounded by spaces on either side (although the first word of the paragraph need not be preceded by a space, and the last word of the paragraph need not be followed by a space). Thus the first word of the problem statement that you are reading now is "KDE," including the comma, and the fourth word is "it's".

You must arrange the words into lines so that each line except for the last contains as many words as possible. Words within each line must be separated by at least one space, and the total length of each line (including spaces) must not exceed the screen width w.

Of course some lines may have total length less than w, and so the question remains how to display such lines on the screen. You will be asked to use one of three styles of output: left flushed, right flushed and justified. These styles of output are explained below, and are illustrated in the sample data later in this question statement.

Input

Each input file will contain a single paragraph and a specification for how it is to be output.

The first line of input will be of the form s w, where s is a single upper-case letter representing the output style and w is the screen width. The letter s will be either L, R or J, for left flushed, right flushed and justified output respectively. You are guaranteed that 3 <= w <= 70.

Following this will be some number of lines containing the actual text of the paragraph. The final line of input will contain a single hash character (#). This hash is not considered to be part of the paragraph text.

The paragraph text will contain between 1 and 1000 lines inclusive. Each of these lines will contain at least one word and be at most 80 characters long. None of these lines will contain any hash characters or tabs. If m is the length of the longest word in the paragraph text, you are guaranteed that 2m+1 <= w.

Output

The output must contain the paragraph text laid out in the correct style as described above. Lines may contain leading spaces as required by the output style. Lines must not contain any trailing spaces. That is, there must be no spaces output after the final word on each line. The output must not contain any blank lines.

Sample Input 1

L 66
You have been hired     by a company
which needs to place   an information
  booth in a shopping mall.  The shopping mall also
contains four fake trees to add   ambiance   to a shopper's experience.
#

Sample Output 1 (Left Flushed)

You have been hired by a company which needs to place an
information booth in a shopping mall. The shopping mall also
contains four fake trees to add ambiance to a shopper's
experience.

Sample Input 2

R 66
You have been hired     by a company
which needs to place   an information
  booth in a shopping mall.  The shopping mall also
contains four fake trees to add   ambiance   to a shopper's experience.
#

Sample Output 2 (Right Flushed)

          You have been hired by a company which needs to place an
      information booth in a shopping mall. The shopping mall also
           contains four fake trees to add ambiance to a shopper's
                                                       experience.

Sample Input 3

J 66
You have been hired     by a company
which needs to place   an information
  booth in a shopping mall.  The shopping mall also
contains four fake trees to add   ambiance   to a shopper's experience.
#

Sample Output 3 (Justified)

You  have  been  hired  by  a  company  which  needs  to  place an
information  booth  in  a  shopping  mall.  The shopping mall also
contains   four   fake  trees  to  add  ambiance  to  a  shopper's
experience.

Scoring

The score for each input file will be 100% if the correct answer is written to the output file and 0% otherwise.

Note: Unlike most problems, leading and trailing spaces on each line will be taken into account by the judging software. You should ensure that each line contains precisely the right number of leading spaces and that each line contains no trailing spaces.

 


Privacy statement
© Australian Mathematics Trust 2001-2019

Contact: training@orac.amt.edu.au
Page generated: 26 August 2019,  4:36am AEST