#K43762. Directory Tree Navigator
Directory Tree Navigator
Directory Tree Navigator
You are given a simplified representation of a directory tree. The tree always starts at the root directory named root and may contain nested subdirectories and files. You need to simulate a basic file system navigator that supports the following commands:
cd <directory_name>
: Move into the subdirectory with the given name (if it exists within the current directory).cd ..
: Move back to the parent directory (if the current directory is not the root).ls
: List the immediate items (files or directories) in the current directory in lexicographical order.
After processing the commands, for each ls
command, you are to output the resulting list with each item on its own line, followed by an empty line. The navigator must ignore attempts to move above the root directory. Note that the file system is static and is built based on a collection of paths provided in the input.
The internal structure of the directory tree can be conceptualized as a recursive mapping. In fact, if you imagine a node representing a directory, then its children correspond to the list of directories or files immediately under it. If a command is issued that changes the directory state, subsequent commands must be executed with respect to that state.
For example, if the directory tree is given by:
\( \texttt{root/dir1} \), \( \texttt{root/dir1/file1} \), \( \texttt{root/dir1/file2} \), \( \texttt{root/dir2} \), \( \texttt{root/dir2/file3} \) ... and the commands are executed in sequence, your program should output the proper listings at eachls
command.</p>
inputFormat
The input is read from stdin and has the following format:
- An integer n --- the number of paths in the directory tree.
- n lines, each containing a path. Each path is a string of tokens separated by '/' (the first token is always
root
). - An integer m --- the number of commands.
- m lines, each containing one command. Commands are one of the following:
cd <directory_name>
,cd ..
, orls
.
outputFormat
The output is written to stdout. For each ls
command, print the items (files or directories) in the current directory in lexicographical order, one per line. After printing the list for each ls
command, print an empty line. If the current directory is empty, just print an empty line.
10
root/dir1
root/dir1/file1
root/dir1/file2
root/dir2
root/dir2/file3
root/dir2/file4
root/dir2/subdir1
root/dir2/subdir1/file5
root/file6
root/file7
8
ls
cd dir1
ls
cd ..
cd dir2
ls
cd subdir1
ls
dir1
dir2
file6
file7
file1
file2
file3
file4
subdir1
file5
</p>