← 返回 perplexity 的题目列表Command-Line In-Memory File System
类型:online_judge
Problem: Command-Line In-Memory File System
Implement a simplified Unix-like file system that runs entirely in memory. You need to read command lines from standard input, parse them, and execute them.
Initially, the file system only contains the root directory /.
Input Format
The first line contains an integer Q, the number of commands.
The next Q lines each contain one command.
Path Rules
All paths are absolute paths starting with /.
The root directory is /.
Path components contain only letters, digits, underscore _, hyphen -, and dot ..
A directory and a file cannot have the same name under the same parent.
Supported Commands
1. mkdir PATH
Create directories recursively, similar to mkdir -p.
Example:
mkdir /a/b/c
If intermediate directories do not exist, create them automatically.
No output.
2. touch PATH
Create an empty file.
If the file already exists, do not change its content.
If the parent directory does not exist, print ERROR.
If the same path already exists as a directory, print ERROR.
3. ls PATH
List the content of a path.
If PATH is a file, print the file name.
If PATH is a directory, print all immediate child names in lexicographical order, separated by one space.
If the directory is empty, print an empty line.
If the path does not exist, print ERROR.
4. echo "CONTENT" > PATH
Write CONTENT to a file, overwriting its previous content.
If the file does not exist, create it.
The parent directory must exist.
If PATH already exists as a directory, print ERROR.
CONTENT may contain spaces; if it contains spaces, it will be quoted.
5. echo "CONTENT" >> PATH
Append CONTENT to the end of a file.
The rules are the same as above, except existing content is preserved.
6. cat PATH
Print the content of a file.
If the file is empty, print an empty line.
If the path does not exist or is a directory, print ERROR.
7. rm PATH
Delete a file or directory.
If it is a directory, delete the entire subtree recursively.
Deleting the root directory / is not allowed; print ERROR.
If the path does not exist, print ERROR.
Output Format
For every command that produces output, print one line.
Constraints
1 <= Q <= 2 * 10^4
Each command line has length at most 1000
The total number of file-system nodes is at most 10^5
The total length of file contents is at most 10^6
Example
Input:
7
mkdir /a/b
touch /a/b/file
ls /a/b
echo "hello" > /a/b/file
echo " world" >> /a/b/file
cat /a/b/file
ls /a/b/file
Output:
file
hello world
file
Example
Input
7
mkdir /a/b
touch /a/b/file
ls /a/b
echo "hello" > /a/b/file
echo " world" >> /a/b/file
cat /a/b/file
ls /a/b/file
Output
file
hello world
file