13907 - Shell Directory Stack   

Description

As a CS student, you must be familiar with command-line shell, especially the basic concept of working directory and th cd command. In fact, modern shells (even Windows' Command Propmt included!) maintain a stack (a container that holds FILO (First-In, Last-Out) principle) of directories so that we could traverse among directories easily. For the purpose of understanding the mechanism well, let's simulate the fundamental behaviors.

The paths in Unix-like OS could be divided into 2 types:

  • Absolute path. Start with the root directory / and the other directories within the path (if any) are splited by /.
  • Relative path. Start with one of the following terms and the other directories within the path (if any) are splited by /.
    • ~ means ones's home directory. Here suppose we're running as super user root thus it equals /root.
    • . denotes the current working directory.
    • .. represents the parent directory of current working directory. Note that if current working directory is root directory, then it's still the root directory.

For instance, ~/.ssh could be expanded to /root/.ssh.

The commands involved are listed below. Though some details vary among shells (e.g., bash or zsh) and this problem is mainly based on bash, slight difference might exist. These commands below support many other useful parameters yet too complex to implement.

  • cd. Change directory.
    • If a path is given as argument than change working directory to it.
    • Else if the argument is -, change back to previous ("old") working directory.
    • Otherwise if there's no argument, change to the home directory, namely /root here.
  • pwd. Print working directory.
    • No argument.
  • pushd. Push directory into stack.
    • You may think of that we push current working directory into the stack, then change working directory just like cd.
    • If no argument is given, swap working directory with the top of the stack.
      • If the stack is empty, print "pushd: no other directory" (without quotation).
  • popd. Pop diectory from stack.
    • Change working directory to the top of the stack.
      • If the stack is empty, print "popd: directory stack empty​" (without quotation).
  • dirs.
    • Show current working directory and the directories in the stack from top.

Initally, the working directory is the home directory, /root, and we set the old working directory same.

Input

There would be at most 5000 commands. The name of each directory consists of only English letters and numbers, and the length doesn't exceed 10.

About 20% of the testcases contain only absolute paths. Another approximate 30% of the testcases don't containe paths related to .. parent of current working directory.

Assume that all paths are valid.

Output

Already mentioned in above description. Please note that for dirs command, redudant space at the end of line is not allowed.

Sample Input  Download

Sample Output  Download

Tags




Discuss