24. Text justification

The thaw had come early that year, loosening the river ice and softening the air above Dorpat. Meltwater dripped from the eaves of the printing house, each drop leaving small crescents in the soot on the sill. Inside, the presses no longer groaned—they sang, steady and tireless, like a heartbeat that had finally found its rhythm.

Karl stood near the compositor’s table, a folded proof clenched in his ink-stained hand. „Look at this,“ he said, stabbing a finger at the page. „The left side is a fortress, but the right—ragged as a ploughed field. If we’re to print essays now, not verse, the columns must stand straight.“

Liina, sleeves rolled to the elbow, set down her pen. „And if the lines breathe unevenly?“ she asked. „Must every thought be pressed until it fits your ruler?“

Karl sighed, half in exasperation, half in pride. „Readers trust what looks precise. The Germans print with perfect edges. Crooked text looks like crooked thinking.“

Mihkel had been silent until then, standing by the window where sunlight pooled on the floorboards. „Perhaps it needn’t be crooked or cramped,“ he said quietly. „Each line could share its empty space—so that none bears too much or too little.“

Liina tilted her head. „Share its space?“

He nodded. „Like verses in a choir. Not forced to sing alike, but held in balance by the silence between them.“

On the input tape, you’ll get a multi-line text (from Quest 21). Your task is to justify each line, so all of them have the same length.

The text consists of 1 or more lines (separated by +). Each line consists of 1 or more words (separated by -). Each word consists of English letters (a-z). The line length includes the in-line hyphens (-) but not line breaks (+).

Justification means distributing extra - characters between words so that the line reaches the required target length. The extra - characters must be distributed as evenly as possible across all gaps between words in the line: every gap gets at least the same base number of -, and the first few gaps (from the left) get one additional - if the total number of extra - characters doesn’t divide evenly (see the examples below).

Examples

If the input tape is hi+world, your output tape should be hi---+world (each line is 5 characters).

If the input tape is ab-b-c-d-et+f-g-h-i-j+ktk, your output tape should be ab-b-c-d-et+f--g--h-i-j+ktk-------- (each line is 11 characters).

Sign in to submit your solution.