Lister
SRM 196 · 2004-05-27 · by dgoodman
Problem Statement
Our list will be printed out row by row, with spaces inserted to position the names within each row properly. Each row must contain exactly pageWidth characters (including spaces). In order to avoid run-together, we require that only spaces can appear in the print position just prior to the start of a column. This applies even to the last row (where the final columns may contain no names).
Create a class Lister that contains a method doList that is given
pageWidth and a
A listing is "optimal" if it has as few rows as possible. Among listings with the minimal number of rows, choose the one whose rightmost non-space character is as far to the left as possible. If multiple listings are still equally "optimal" choose the one with the fewest columns.
Constraints
- pageWidth will be between 10 and 80 inclusive.
- names will contain between 1 and 50 elements inclusive.
- Each element of names will contain between 1 and pageWidth characters inclusive.
- Each element of names will contain only lowercase letters 'a'-'z'.
10
{"bob","abernathy","x"}
Returns: { "abernathy ", "bob ", "x " }
Any column containing "abernathy" cannot be adjacent to another column since even a one-character name would be too much to avoid run-together and fit on the page (spaces shown as '.'): abernathy. bob....... x.........
11
{"bob","abernathy","x"}
Returns: { "abernathy x", "bob " }
There is just enough page width to get abernathy and x on the same row and keep them from running together, producing the following listing (spaces shown as '.'): abernathy.x bob........
10
{"bob","a","x"}
Returns: { "a bob x " }
10
{"a","b","a","c","d","ab","e","f","g"}
Returns: { "a ab c e g", "a b d f " }
18
{"a","b","a","c","d","ab","e","f","g"}
Returns: { "a a ab b c d e f g" }
10
{"x","teddy","andy"}
Returns: { "andy x ", "teddy " }
Note that there are 2 spaces between "andy" and "x" in the listing. It would be illegal to have only 1 space, since then the 'y' in "teddy" would occupy the print position just prior to the second column of names (spaces shown as '.'): andy..x... teddy.....
Submissions are judged against all 25 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Lister with a public method vector<string> doList(int pageWidth, vector<string> names) · 25 test cases · 2 s / 256 MB per case