Disk Tree

Interview

Time limit1sMemory limit128 MB

Summary
Given full directory paths, rebuild the tree and print every directory name on its own line, indented by depth, with siblings in ASCII order.
Level

Medium4 of 10

Topics
Trie, Sorting, DFS
Solved
No attempts yet

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 NN (1≤N≤5001 \le N \le 500). Each of the next NN lines contains one directory path.

  • Each path is a single-line string that contains no spaces and is at most 8080 characters long.
  • Within a path, directories are separated by a backslash \.
  • Each directory name is between 11 and 88 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.

Examples3

  1. Example 1

    Input
    7
    WINNT\SYSTEM32\CONFIG
    GAMES
    WINNT\DRIVERS
    HOME
    WIN\SOFT
    GAMES\DRIVERS
    WINNT\SYSTEM32\CERTSRV\CERTCO~1\X86
    
    Expected output
    GAMES
     DRIVERS
    HOME
    WIN
     SOFT
    WINNT
     DRIVERS
     SYSTEM32
      CERTSRV
       CERTCO~1
        X86
      CONFIG
    
  2. Example 2

    Input
    1
    A
    
    Expected output
    A
    
  3. Example 3

    Input
    1
    A\B\C\D\E
    
    Expected output
    A
     B
      C
       D
        E