#K78617. Simulated File System
Simulated File System
Simulated File System
You are required to implement a simple text-based file system that supports the following operations:
mkdir <path>
: Create directories along the given absolutepath
.create <path> <content>
: Create a file at the given absolutepath
with the provided content. (For this problem, thecontent
is given but not used except for the purpose of creation.)ls <path>
: List the contents of the directory atpath
. Ifpath
refers to a file, simply output its own name.
The file system is initialized with only a root directory /
. When a directory or file is created, any missing intermediate directories should be automatically created. For the ls
operation, if the target is a directory, you should output the list of file and directory names (sorted in lexicographical order) in one line separated by a single space. If the directory is empty, output an empty line. If the target is a file, output its name on a single line.
Note: All paths are absolute and use the forward slash /
as the directory separator. There is no explicit deletion or modification operation.
You have to simulate the file system operations by reading from standard input (stdin) and printing results for each ls
operation to standard output (stdout).
In mathematical terms, if we denote the file system as a tree \( \mathcal{T} \) where each directory is a node and each file is a leaf, then the ls
operation on a node \( N \) outputs \( \{ key_1, key_2, \dots, key_k \} \) where \( key_i \) are the identifiers (names) of the children of \( N \) sorted in lexicographical order. If \( N \) is a file (a leaf), then output \( \{ key_N \} \).
inputFormat
The input begins with a single integer T
representing the number of operations. The following T
lines each contain one operation in one of the following forms:
mkdir <path>
create <path> <content>
ls <path>
All paths are absolute and consist of lowercase letters and the /
character. The content
for the create operation is a string without spaces.
outputFormat
For each ls
operation, output a single line representing the list of names in the specified directory, sorted in lexicographical order and separated by a single space. If the target is a file, output its own name. If the directory is empty, output an empty line.
2
mkdir /a/b/c
ls /
a
</p>