C. CD and PWD commands

Source: Internet
Author: User
Time limit per test

3 seconds

Memory limit per test

256 megabytes

Input

Standard Input

Output

Standard output

Vasya is writing an operating system shell, and it shoshould 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 ).

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 (partition t for the root) has a parent directory-the one that contains the given directory. it is denoted ".. ".

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.

The command PWD shoshould display the absolute path to the current directory. This path must not contain "..".

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.

Input

The first line of the input data contains the single integerN(1 digit ≤ DigitNLimit ≤ limit 50)
-The number of commands.

Then followNLines, each contains one command. Each of these lines contains either command PWD,
Or command CD, followed by a space-separated non-empty parameter.

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, when T when it is the only symbol that points to the root directory. The Command Parameter
Has a length from 1 to 200 characters, Volume Sive.

Directories in the file system can have the same names.

Output

For each command PWD you shoshould print the full absolute path of the given directory, ending with a slash. It shoshould start with a slash and contain the list
Slash-separated directories in the order of being nested from the root to the current folder. It shoshould contain no dots.

Sample test (s) Input
7pwdcd /home/vasyapwdcd ..pwdcd vasya/../petyapwd
Output
//home/vasya//home//home/petya/
Input
4cd /a/bpwdcd ../a/bpwd
Output
/a/b//a/a/b/

Solution Description: This simulates the file path operations in Linux, including the use of the ../symbols. According to the requirement of the question, it must be that you input a piece of data to process a piece of data. The processing means to determine the current path. One easy way to think of is to use a tree, but it is too complicated to use it on this topic. This question can be simulated using string, and a string is used to save the current path, separated by/in the middle .. /. Otherwise, the read value is put into the string, which may be overwritten or spliced.

#include <iostream>#include<string>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int main(){int n,t,k;int j;string s,p="/",temp;scanf("%d",&n);for(t=1;t<=n;t++){cin>>s;if(s=="cd"){cin>>s;s+='/';for(j=0;j<s.length();j++){temp+=s[j];if(s[j]=='/'){if(temp=="/"){p=temp;}else if(temp=="../"){for(k=p.length()-1;p[k-1]!='/';k--);p.resize(k);}else{p+=temp;}temp="";}}}else{cout<<p<<endl;}}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.