C. Cd and pwd commands

來源:互聯網
上載者: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 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).

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 "..".

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 should 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 integer n (1 ≤ n ≤ 50)
— the number of commands.

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.

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.

Directories in the file system can have the same names.

Output

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.

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/

解題說明:這題類比了linux下對檔案路徑的操作,包括 .. /這些符號的使用。按照題目要求,肯定是輸入一條資料就處理一條資料,處理的意思就是確定當前的路徑。一個很容易想到的方法是用樹來實現,不過用在這種題目上過於複雜了。這題完全可以用string來類比,用一個string來儲存當前的路徑,中間用/隔開,當出現 ../時回退到上一個/所在處,否則就把讀取到的值放入string,可能是覆蓋或是拼接。

#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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.