← 返回 perplexity 的题目列表Design an In-Memory File System with Shell Commands
类型:online_judge
Implement an in-memory Unix-like file system with directories, regular files, and a current working directory.
Initially, the file system contains only the root directory /, which is also the current working directory. Execute Q commands in order and print one result for each command.
Supported commands:
mkdir path: Create a directory. path may be absolute (starts with /) or relative to the current working directory. Missing intermediate directories are created. It also succeeds if the target directory already exists.
touch path: Create an empty file. Its parent directory must already exist. It succeeds if the file already exists, but fails if a directory with that name exists.
ls [path]: List directory entries in lexicographical order, separated by one space. Without path, list the current directory. If path is a file, print only that file's name.
rm path: Delete a regular file. Fail if the path does not exist or denotes a directory.
rmdir path: Delete an empty directory. The root cannot be deleted. Fail if the target does not exist, is a file, or is non-empty.
cd path: Change the current working directory. The destination must be a directory. Support . and ..; applying .. at root remains at root.
pwd: Print the canonical absolute path of the current working directory.
Consecutive / characters are treated as one separator. Every command has valid syntax, but a referenced path may not exist or may have the wrong type.
For mutating commands (mkdir, touch, rm, rmdir, and cd), print OK on success and ERROR on failure.
Input
The first line contains integer Q.
The following Q lines each contain one command. ls may omit its argument; all other path-taking commands have exactly one path argument.
Output
Print one result per command.
Constraints
1 <= Q <= 100000
Each path has length at most 1000.
The total length of all paths is at most 1000000.
A file or directory name contains no / and is never . or ...
Example
Input
6
mkdir /docs/projects
touch /docs/readme
ls /docs
ls /docs/readme
pwd
ls /
Output
OK
OK
projects readme
readme
/
docs