(ENIGMA-0x02 (2024) - S3 (The old typewriter)

View as PDF

Submit solution

Points: 100 (partial)
Time limit: 1.0s
Memory limit: 64M

Authors:
Problem type
Allowed languages
Blockly, C, C++, Java, Pascal, Python

The Old Typewriter

At Christmas, all the families gather for food and many, many, stories; old accomplishments of the elders, tricks of the young, and the antics of Koula, the neighbor.
Isn't it a shame if these aren't recorded?

This is how Totos started secretly writing down the stories he hears.
But he didn't want to take out a laptop next to the turkey, so he used a simple notebook.
Later, Annoula asked him to type the stories out on their grandfather's old typewriter!

The typewriter uses a paper of special size that fits up to K (20 \le K \le 200) characters per line (we assume all characters, including spaces, have the same size).
Unfortunately, their grandfather had broken the hyphen (-) key, so you cannot break a word into two parts.
Therefore, Totos needs to reformat the notes, changing where to break the text into lines, so that all lines fit within the paper size of the typewriter.

We want to write a program that will read from STDIN the integer K, the maximum line size of the paper, followed by lines containing words.
Each word has a length of at most 20 characters.
The program will print to STDOUT each word from the input with appropriate line breaks so that each line contains at most K characters.
If a line contains fewer than K characters, the text should be left-aligned, as shown in the example.

Attention: The first line should begin with 4 spaces (' '), as it marks the beginning of a paragraph.

Subtasks
  • The implementation of the above description will be graded for 70% of the points.
  • For an additional 20% of the points, you need to structure the text to have separate paragraphs.
    When reading the word PAR, you should not print it, and instead leave a blank line.
    Each paragraph (including the first) should begin with 4 spaces.
  • For an additional 10% of the points, the input may contain a word "split" into 2 lines using a hyphen (-); you should print these words together in the output.
Example

Input (STDIN)

42
GENTLE READER: This is a handbook about TeX, a new typesetting system
intended for the creation of beautiful books-and especially for books
that contain a lot of mathematics. By preparing a manuscript in TeX format,
you will be telling a computer exactly how the manuscript is to be transformed
into pages whose typographic quality is comparable to that of the world's finest
printers; yet you won't need to do much more work than would be involved if
you were simply typing the manuscript on an ordinary typewriter. In fact, your
total work will probably be significantly less, if you consider the time it ordinarily
takes to revise a typewritten manuscript, since computer text files are so easy
to change and to reprocess. (If such claims sound too good to be true, keep in
mind that they were made by TeX's designer, on a day when TeX happened to
be working, so the statements may be biased; but read on anyway.)

Output (STDOUT)

    GENTLE READER: This is a handbook
about TeX, a new typesetting system
intended for the creation of beautiful
books-and especially for books that
contain a lot of mathematics. By preparing
a manuscript in TeX format, you will be
telling a computer exactly how the
manuscript is to be transformed into pages
whose typographic quality is comparable to
that of the world's finest printers; yet
you won't need to do much more work than
would be involved if you were simply
typing the manuscript on an ordinary
typewriter. In fact, your total work will
probably be significantly less, if you
consider the time it ordinarily takes to
revise a typewritten manuscript, since
computer text files are so easy to change
and to reprocess. (If such claims sound
too good to be true, keep in mind that
they were made by TeX's designer, on a day
when TeX happened to be working, so the
statements may be biased; but read on
anyway.)
A Small Cheat Sheet!

Reading an indefinite number of words from input: To read the words following the number K from STDIN, you can use the following code:

Python:

import sys

for word in sys.stdin.read().split():
  # The variable word stores 1 new word from the input

C/C++ (with cstdio):

#include <stdio.h>

char word[21]; // maximum word length + 1
while (scanf("%s", word) == 1) {
  // The variable word stores 1 new word from the input
}

C++ (with iostream):

#include <iostream>

string word;
while (cin >> word) {
  // The variable word stores 1 new word from the input
}

To print in Python without changing the line immediately after, use the end='' parameter in print(), like this:

print(..., end='')

Comments

There are no comments at the moment.