Consider the problem of laying out text in lines of a fixed maximum width L (a.k.a., ``line filling'').
If you do a poor job,
the ends of the lines are unnecessarily
ragged - like
this paragraph. Now,
by convention, we allow the last line of a paragraph to be arbitrarily ragged. We don't mind if that final line contains just a few characters, but we expect the earlier lines to be of approximately uniform length, filling up the column in which we are setting the text.
See if we care.could be laid out in L = 6 like this:
See if we care.That layout is, arguably, not as visually pleasing as
See if we care.Define a ``word'' as any sequence of non-whitespace characters bounded by a line start or end or by a blank. The legal ``whitespace characters'' in this problem are blanks and the line terminator characters.
Given a sequence of N words of width w1, w2,..., wN, and a maximum line width L, with the guarantee that for all i, wi
Then we can define the raggedness of a line containing words i though j as
r(i, j) = (L - w(i, j))2
Write a program to read paragraphs of text and to lay them out in a
way that no line contains more than L characters, for a specified
L, and so that you minimize the total raggedness added up over all
lines except the last one. (The final line of a paragraph can be
arbitrarily shorter then the lines above it.) Line terminator
characters are not counted as part of the line width.
Input
Input will consist of one or more datasets.Each dataset begins with a line containing one integer, L, denoting the maximum line width (not counting line terminator characters). You are guaranteed that 0 < L
The remainder of the dataset consists of up to 250 lines containing a paragraph of text, terminated by an empty line. Paragraphs may contain from 1 to 500 words, where a word is any consecutive sequence of non-whitespace characters.
No line of text will contain a word of length greater than L.
Output
Print each paragraph laid out optimally as described above. After each paragraph print a line containing ``==='' (three equal signs).If there is more than one way to fill a paragraph with the optimal raggedness, any such layout may be printed.
Sample Input
6 See if we care. 25 Raggedy, raggedy are we. Just as raggedy as raggedy can be. We don't get nothin' for our labor. So raggedy, raggedy are we. - P Seeger 0
Sample Output
See if we care. === Raggedy, raggedy are we. Just as raggedy as raggedy can be. We don't get nothin' for our labor. So raggedy, raggedy are we. - P Seeger ===
Como veran el problema no es tan sencillo. Las unicas observaciones que hemos hecho hasta el momento son las siguientes:
ResponderEliminarLa idea de hacer el justificado tradicional, tratar de poner la mayor cantidad de palabras en cada renglon nos da la cantidad de renglones del acomodo que pide el problema (aumentar los renglones seria incrementar la suma de los r(i,j)), asi como la cantidad total de espacio desperdiciado en total, sin elevar al cuadrado..