Disk Tree
InterviewTime limit1sMemory limit128 MB
Given full directory paths, rebuild the tree and print every directory name on its own line, indented by depth, with siblings in ASCII order.
Problem
One day a laptop suddenly refused to power on. Fortunately, the full paths of the important directories inside it had been saved separately in a text file beforehand, for example WINNT\SYSTEM32\CERTSRV\CERTCO~1\X86.
Given the full paths of all the important directories, write a program that reconstructs the directory structure (tree) from those paths and prints it in a readable form.
Input
The first line contains the number of full directory paths (). Each of the next lines contains one directory path.
- Each path is a single-line string that contains no spaces and is at most characters long.
- Within a path, directories are separated by a backslash
\. - Each directory name is between and characters long and consists of uppercase letters, digits, and special characters.
- The special characters that may appear in a directory name are
!#$%&'()-@^_`{}~.
Output
Print the reconstructed directory structure in a readable form, following these rules.
- Print one directory name per line.
- The number of leading spaces on a line indicates that directory's depth. Top-level (root) directories are printed with no leading spaces.
- A directory's children are printed with exactly one more leading space than their parent.
- Children that share the same parent are printed in ascending lexicographic (ASCII code) order of their names.
Hint
This story is a reinterpretation of a true event; the next morning the laptop turned on again.