Hidden Hierarchy
InterviewTime limit1sMemory limit512 MB
Build a directory tree from file paths, and print the smallest set of directories (expanding or collapsing as needed) that covers every directory whose total size is at least t.
Problem
You are building the user interface of a simple text based file explorer. One of your tasks is a navigation pane that displays the directory hierarchy. The filesystem consists of directories that hold files and other directories, and those directories again hold files and directories, so the directories form a hierarchical tree. The topmost directory of the hierarchy is the root directory. If directory directly holds directory , then is the parent directory of and is a subdirectory of . Every file has a size in bytes. The size of a directory is the total size of all files held directly or indirectly inside it.
Every file and every directory except the root has a name, a string that always starts with a lowercase letter and consists of lowercase letters and the "." (dot) character only. Items (files and directories) placed directly inside the same parent directory must have distinct names. Every item is described uniquely by its path, built with the following rules.
- The path of the root directory is a single slash, "/".
- The path of a directory is obtained by concatenating the directory names top to bottom along the hierarchy from the root directory down to , putting a "/" character in front of each name and one more "/" character at the end.
- The path of a file is the path of its parent directory followed by the name of .
The hierarchy is displayed by printing the root directory. Printing a directory means writing one line of the form " ", where and are the path and the size of , and is its expansion marker, explained below. If holds other directories, you must choose either to collapse it or to expand it. If you expand , you print all of its subdirectories in lexicographical order by name, using the same rules. If you collapse , you simply ignore its contents.
The expansion marker is a single blank character when has no subdirectory at all, the "+" (plus) character when you collapse , and the "-" (minus) character when you expand .
Given a list of files and a threshold integer , display the directory hierarchy so that every directory of size at least is printed and the total number of printed directories is as small as possible. Assume there are no empty directories, so the whole hierarchy follows from the given file paths. The root directory is printed no matter what its size is. A directory of size at least only has to be printed, it does not have to be expanded.
Input
The first line contains an integer (), the number of files. Each of the following lines contains the path and the size () of a single file, separated by a space. Each path is at most 100 characters long and is a valid file path under the rules above. All paths are different.
The next line contains an integer (), the threshold directory size.
Output
Print the display of the hierarchy for the given threshold that uses the smallest number of printed directories, as described above. Each line holds the expansion marker, one space, the path, one space, and the size, in that order.