Filesystem
Time limit1sMemory limit1024 MB
Simulate a small file system with echo, cp, mv, rm, mkdir, and rmdir commands, then print each queried file's contents or invalid!.
- Level
Medium4 of 10
- Topics
- Simulation, String, Tree
- Solved
- No attempts yet
Problem
Given a series of file system commands and queries, output the contents of the file requested by each query. Each query is a filename to print. If a query is invalid (for example, its path does not exist), print invalid!.
Support the following commands:
echo "<content>" > <path>: Writes<content>to the file at<path>. If<path>exists, it must be a file, and its contents are replaced. If it does not exist, its parent directory must exist, and the file is created.<content>contains only ASCII digits, letters,*,?,<,>, and spaces.cp <source> <destination>: Copies the file<source>to<destination>. If<destination>already exists and is a directory, the file is copied into that directory under the final path component of<source>. Otherwise,<destination>is the new filename, and its parent directory must already exist. The new file replaces any existing content.mv <source> <destination>: Moves the file or directory<source>to<destination>. As withcp, if<destination>already exists and is a directory, the source is moved into it under the final path component of<source>. Otherwise,<destination>is the new name, and its parent directory must already exist. The new file replaces any existing content, and<source>is removed.rm <path>: Deletes the file at<path>.mkdir <path>: Creates a directory at<path>. The parent directory must already exist.rmdir <path>: Removes an empty directory at<path>.
All paths contain only digits, letters, or /. A path is at most 31 characters and has at most 7 components, separated by /. All commands in the input are valid. Unless specified above, no command refers to a path that does not exist or is invalid. Each command is shorter than 1024 characters.
Input
The first line contains T (0 < T < 100), the number of test cases.
Each test case starts with a line containing C (0 < C < 100), the number of commands. The next C lines contain the commands that build the filesystem. The starting folder may be ignored, so the current directory (./) can be assumed.
The next line contains Q (0 < Q < 20), the number of filenames to print. The next Q lines contain the filenames, which may include a path such as myfolder/myfile.
Output
For each query, print the contents of the requested file, or invalid! if the file does not exist. Print a blank line after each test case.