9596. Word processor
Bessie is working on an essay for her
writing class. Since her handwriting leaves much to be desired, she decides to
type the essay using a word processor.
The essay consists of n words separated by spaces. Each word has a length
between 1 and 15 characters
inclusive and contains only lowercase or uppercase Latin letters. According to
the assignment instructions, the essay must be formatted in a very specific
way: each line must contain at most k characters, excluding spaces. Fortunately,
Bessie’s word processor can enforce this requirement in the following manner:
·
If,
when adding the next word, it fits on the current line, the word is appended to
that line.
·
Otherwise,
the word is moved to the next line, and filling continues there.
Naturally, any two consecutive words on the
same line must be separated by exactly one space. No line may end with a space.
Unfortunately, Bessie’s word processor has
just broken. Help her format the essay according to the rules described above.
Input. The first line contains two integers n (1 ≤ n ≤ 100) and k (1 ≤ k
≤ 80). The second
line contains n words. No word exceeds k characters, which is the
maximum allowed number of non-space characters in a single line.
Output. Print Bessie’s essay, formatted properly.
|
Sample
input |
Sample
output |
|
10 7 hello my name is
Bessie and this is my essay |
hello my name is Bessie and this is my essay |
greedy
Read
the input words one by one. The current word is printed on the current line if
the total length of the words printed on that line does not exceed k. If
adding the word would make the line length greater than k, the word is
printed on a new line.
Read the input words into an array s.
char s[100];
Read the input
data.
scanf("%d %d", &n, &k);
The variable ptr
stores the current length of the output string.
ptr = 0;
Read the n
words sequentially.
while (n--)
{
Read the current word
s and find its length len.
scanf("%s", s);
len = strlen(s);
If ptr + len
≤ k, then the word s can
be printed on the current line. The length of the output string will not exceed
k characters.
if (ptr + len
<= k)
{
ptr += len;
printf("%s
", s);
}
Otherwise, the
word s should be printed on a new line. Set the value of ptr for
the new line to len.
else
{
ptr = len;
printf("\n%s
", s);
}
}
Read the input data.
cin >> n >> k;
The variable ptr stores the current length of the output string.
ptr = 0;
Read the n words sequentially.
while (n--)
{
Read the current word s and find its length len.
cin >> s;
len = s.size();
If ptr + len ≤ k, then the word s can be printed on the current line. The
length of the output string will not exceed k characters.
if (ptr + len <= k)
{
ptr += len;
cout << s << " ";
}
Otherwise, the word s should be printed on a new line. Set the
value of ptr for the new line to len.
else
{
ptr = len;
cout << endl << s << "
";
}
}
Read the input data.
n, k = map(int, input().split())
s = input().split()
The variable ptr stores the current length of the output string.
ptr = 0
Iterate through the words w
in the list s.
for w in s:
Find the length of the word w.
lw = len(w)
If ptr + lw ≤ k, then the word w can be
printed on the current line. The length of the output string will not exceed k
characters.
if ptr + lw <= k:
ptr += lw
print(w, end=" ")
Otherwise, the word w should
be printed on a new line. Set the value of ptr for the new line to lw.
else:
ptr = lw
print()
print(w, end=" ")