Unix ls Column Formatting
Time limit1sMemory limit128 MB
Sort filenames by ASCII value and lay them out in column-major order with fixed-width padding, as few rows as fit in 60 characters.
- Level
Easy3 of 10
- Topics
- Sorting, Implementation, Simulation, String
- Solved
- No attempts yet
Problem
You are given a list of filenames. Sort the list in ascending order by ASCII character value, then, like the Unix ls command, arrange and print the names in several columns.
Each filename is between 1 and 60 characters long. Let be the length of the longest filename. The output follows these rules:
- Every column is left-justified.
- Each filename is printed left-justified in a field of width . This padding applies to every column, including the last one, so each output line ends with trailing spaces.
- The number of columns that fit on a line is the largest value satisfying ; that is, even the last column's actual content width must fit within 60 characters.
- Use as few rows as possible. The sorted filenames fill each column from top to bottom: names through go down column 1, names through down column 2, and so on.
Input
The input consists of several filename lists. Each list begins with a line containing the count (), followed by lines that each contain one filename. The entire content of a line is a single filename, 1 to 60 characters long. The only allowed characters are letters (a–z, A–Z), digits (0–9), and ., _, -. There are no illegal characters and no empty lines. Immediately after the last filename comes either the of the next list or the end of the input. Read and process every list until the input ends.
Output
For each filename list, first print a line of exactly 60 dashes (-), then print the filenames arranged in columns as described above. Filling is column-major: sorted names through are listed down column 1, names through down column 2, and so on.