Book Borders
Time limit2sMemory limit512 MB
For each width m from a to b, wrap the words greedily into lines of at most m characters and report the length of the sentence made of each line's first word.
- Level
Hard8 of 10
- Topics
- Divide and conquer, Prefix sum, Binary search
- Solved
- No attempts yet
Problem
A book is typeset with a fixed width font and a simple greedy algorithm that fills one line at a time. The contents of the book are a sequence of words, and each word has one or more characters.
Before typesetting you choose a maximum line length and call it . A line holds at most characters, counting the spaces between words. The algorithm walks through the words in order and prints exactly one space between two consecutive words on the same line. If adding the current word to the current line would push it past characters, the algorithm starts a new line with that word instead.
|its.a.long...| |its.a.long.way|
|way.to.the...| |to.the.top.if.|
|top.if.you...| |you.wanna.rock|
|wanna.rock.n.| |n.roll........|
|roll.........|
The figure shows the text "its a long way to the top if you wanna rock n roll" typeset with maximum line lengths 13 and 14. The dots stand for space characters.
Fix one value of . Take the first word of every line from top to bottom and join those words with a single space. The result is called the leading sentence. With maximum line length 14 the leading sentence in the figure is "its to you n".
You are given a text and two integers and . For every maximum line length between and inclusive, find the length of the leading sentence. The length of a sentence is the number of characters it contains, counting its spaces.
Input
The first line contains the text to typeset: a sequence of words separated by exactly one space. Each word is a string of one or more lowercase letters of the English alphabet.
The second line contains two integers and , the ends of the interval described above.
Let be the length of the longest word in the text and let be the total number of characters in the text, counting the spaces. Then .
Output
Print lines. The -th line contains one integer, the length of the leading sentence when the maximum line length is .