9596. Word processor

 

Bessie the cow is working on an essay for her writing class. Since her handwriting is quite bad, she decides to type the essay using a word processor.

The essay contains n words, separated by spaces. Each word is between 1 and 15 characters long, inclusive, and consists only of uppercase or lowercase letters. According to the instructions for the assignment, the essay has to be formatted in a very specific way: each line should contain no more than k characters, not counting spaces. Fortunately, Bessie’s word processor can handle this requirement, using the following strategy:

·        If Bessie types a word, and that word can fit on the current line, put it on that line.

·        Otherwise, put the word on the next line and continue adding to that line.

Of course, consecutive words on the same line should still be separated by a single space. There should be no space at the end of any line.

Unfortunately, Bessie's word processor just broke. Please help her format her essay properly!

 

Input. The first line contains two integers n (1 ≤ n ≤ 100) and k (1 ≤ k ≤ 80). The next line contains n words separated by single spaces. No word will ever be larger than k characters, the maximum number of characters on a line.

 

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

 

 

SOLUTION

greedy

 

Algorithm analysis

Read the input words sequentially. The current word is printed in the current line if the total length of words printed in the line does not exceed k. If, when adding a word to the current line, the length of the line becomes greater than k, then the word is printed in a new line.

 

Algorithm realization

Read the input words into the 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 n words sequentially.

 

while (n--)

{

 

Read the next word s and find its length len.

 

  scanf("%s", s);

  len = strlen(s);

 

If ptr + lenk, 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. Let the length ptr of the new current line be equal to len.

 

  else

  {

    ptr = len;

    printf("\n%s ", s);

  }

}

 

Algorithm realization – string

Read the input data.

 

cin >> n >> k;

 

The variable ptr stores the current length of the output string.

 

ptr = 0;

 

Read n words sequentially.

 

while (n--)

{

 

Read the next word s and find its length len.

 

  cin >> s;

  len = s.size();

 

If ptr + lenk, 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. Let the length ptr of the new current line be equal to len.

 

  else

  {

    ptr = len;

    cout << endl << s << " ";

  }

}

 

Python realization

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 + lwk, 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. Let the length ptr of the new current line be equal to lw.

 

  else:

    ptr = lw

    print()

    print(w, end=" ")