← 返回 uber 的题目列表Design an In-Memory File System with Recursive Wildcards
类型:online_judge
Implement an in-memory file system that contains directories only. Initially, the file system has just the root directory /, which is also the current working directory.
Support the following operations:
mkdir(String name)
pwd()
cd(String path)
Operations
mkdir(name)
Create a direct child directory named name under the current directory.
name contains no / and is not ., .., or *.
The operation fails if the child already exists.
pwd()
Return the absolute path of the current directory.
Return / for the root.
Otherwise return a path such as /home/user/docs.
cd(path)
Change the current directory to the directory denoted by path.
Path rules:
A path beginning with / is absolute and is resolved from the root; otherwise it is relative to the current directory.
Repeated / characters are treated as one separator.
. denotes the current directory.
.. denotes the parent directory. Applying .. at the root remains at the root.
* may appear only as a complete path component and matches zero or more directory levels. For example:
/a/*/target can match /a/target, /a/x/target, and /a/x/y/target.
* may match zero levels, i.e. the current directory itself.
Every non-* component must exactly match a directory name.
If wildcard resolution finds no directory or more than one distinct directory, cd fails and the current directory must not change.
Design appropriate data structures and implement the APIs.
For the command-line test format below:
Q
<command 1>
<command 2>
...
<command Q>
Each command is mkdir name, pwd, or cd path. Output rules:
Print OK for a successful mkdir or cd.
Print ERROR for a failed mkdir or cd.
Print the current absolute path for pwd.
Example
Input:
10
mkdir a
mkdir b
cd a
mkdir x
cd /
cd /a/*/x
pwd
cd /a/*
pwd
Output:
OK
OK
OK
OK
OK
OK
/a/x
ERROR
/a/x
Here /a/* matches both /a and /a/x, so that cd operation fails.
Example
Input
3
pwd
mkdir home
pwd
Output
/
OK
/