#C8507. Design a File System
Design a File System
Design a File System
You are given a simplified file system where each file or directory is identified by a unique string path. All paths are created at the root directory (i.e., there are no nested directories in the sense of hierarchical directories, but a created path may contain a single level of nesting, e.g. '/a/b', where '/a' must already exist).
You need to implement two operations:
create <path> <value>
: Creates a new path and assigns it an associated integer value. The creation will be successful only if the path does not already exist and its parent path exists (except for the root). Otherwise, it fails.get <path>
: Returns the value associated with the path if it exists, otherwise returns-1
.
Notes:
- The path is a string beginning with '/' and includes only lowercase letters and '/' characters. The total length of the path does not exceed 100 characters.
- For the create operation, output
true
if the path is successfully created, andfalse
if the path already exists or the parent path does not exist. - For the get operation, output the associated integer value if found, or
-1
otherwise.
inputFormat
The input begins with an integer n representing the number of commands. Each of the following n lines contains a command. A command can be either:
- create <path> <value>: Create a new path with the given value.
- get <path>: Retrieve and output the value associated with that path.
outputFormat
For each command, print the result on a new line. For a 'create' command, output 'true' if the operation is successful, or 'false' if it fails. For a 'get' command, output the integer value associated with the path, or -1 if the path does not exist.## sample
5
create /a 1
get /a
create /a/b 2
get /a/b
get /nonexistent
true
1
true
2
-1
</p>