codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

158C.md (3132B)


      1 Source: https://codeforces.com/problemset/problem/158/C
      2 
      3 C. Cd and pwd commands
      4 
      5 time limit per test
      6 
      7 3 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current directory) and pwd (display the current directory).
     22 
     23 Directories in Vasya's operating system form a traditional hierarchical tree structure. There is a single root directory, denoted by the slash character "/". Every other directory has a name — a non-empty string consisting of lowercase Latin letters. Each directory (except for the root) has a parent directory — the one that contains the given directory. It is denoted as "..".
     24 
     25 The command cd takes a single parameter, which is a path in the file system. The command changes the current directory to the directory specified by the path. The path consists of the names of directories separated by slashes. The name of the directory can be "..", which means a step up to the parent directory. «..» can be used in any place of the path, maybe several times. If the path begins with a slash, it is considered to be an absolute path, that is, the directory changes to the specified one, starting from the root. If the parameter begins with a directory name (or ".."), it is considered to be a relative path, that is, the directory changes to the specified directory, starting from the current one.
     26 
     27 The command pwd should display the absolute path to the current directory. This path must not contain "..".
     28 
     29 Initially, the current directory is the root. All directories mentioned explicitly or passed indirectly within any command cd are considered to exist. It is guaranteed that there is no attempt of transition to the parent directory of the root directory.
     30 
     31 Input
     32 
     33 The first line of the input data contains the single integer *n* (1 ≤ *n* ≤ 50) — the number of commands.
     34 
     35 Then follow *n* lines, each contains one command. Each of these lines contains either command pwd, or command cd, followed by a space-separated non-empty parameter.
     36 
     37 The command parameter cd only contains lower case Latin letters, slashes and dots, two slashes cannot go consecutively, dots occur only as the name of a parent pseudo-directory. The command parameter cd does not end with a slash, except when it is the only symbol that points to the root directory. The command parameter has a length from 1 to 200 characters, inclusive.
     38 
     39 Directories in the file system can have the same names.
     40 
     41 Output
     42 
     43 For each command pwd you should print the full absolute path of the given directory, ending with a slash. It should start with a slash and contain the list of slash-separated directories in the order of being nested from the root to the current folder. It should contain no dots.
     44 
     45 Examples
     46 
     47 Input
     48 
     49 ```
     50 7  
     51 pwd  
     52 cd /home/vasya  
     53 pwd  
     54 cd ..  
     55 pwd  
     56 cd vasya/../petya  
     57 pwd
     58 ```
     59 
     60 Output
     61 
     62 ```
     63 /  
     64 /home/vasya/  
     65 /home/  
     66 /home/petya/
     67 ```
     68 
     69 Input
     70 
     71 ```
     72 4  
     73 cd /a/b  
     74 pwd  
     75 cd ../a/b  
     76 pwd
     77 ```
     78 
     79 Output
     80 
     81 ```
     82 /a/b/  
     83 /a/a/b/
     84 ```