码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 158c

时间:2016-05-10 20:45:43      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

 

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 Input

Input
7
pwd
cd /home/vasya
pwd
cd ..
pwd
cd vasya/../petya
pwd
Output
/
/home/vasya/
/home/
/home/petya/
Input
4
cd /a/b
pwd
cd ../a/b
pwd
Output
/a/b/
/a/a/b/

注:此博客代码来源某只猫:http://www.cnblogs.com/jifahu/p/5471914.html

代码的解释代码主人写的很清楚,这里我就不多说了,但要注意他的代码中用于判断string中的不同分区的设置now的思想是很好的把要处理的数据直接记录并且单点判断(如果是我我会找到‘/‘然后往前找,明显差了一个级别啊),总结来说他的想法就是大区间对小区间的操作是:以记录的形式延迟处理,单点判断。

一下是粘贴到代码:(这题难在读题,题意理解了很好做)

技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<stack>
 4 #include<cstring>
 5 using namespace std;
 6 stack<string>s1;///用于存储
 7 stack<string>s2;///用于输出
 8 int main()
 9 {
10     int t;
11     scanf("%d",&t);
12     while(!s1.empty()) s1.pop();
13     while(!s2.empty()) s2.pop();
14     while(t--)
15     {
16         string op;
17         cin>>op;
18         if(op[0] == p)
19         {
20             cout<<"/";
21             while(!s1.empty())
22             {
23                 s2.push(s1.top());
24                 s1.pop();
25             }
26             while(!s2.empty())
27             {
28                 cout<<s2.top();
29                 s1.push(s2.top());///这个要加上去,因为我不会遍历栈的元素..
30                 s2.pop();
31             }
32             cout<<endl;
33         }
34         else
35         {
36             string a;
37             cin>>a;
38             int leno = a.length();
39             a[leno++] = /;
40             a[leno] = \0;
41             string now;
42             now = "";///取出每个单词,一种三种情况,对应题目的描述
43             for(int i = 0; i < leno; i++)
44             {
45                 now += a[i];
46                 if(a[i] == /)
47                 {
48                     if(now == "../")
49                     {
50                         if(!s1.empty()) s1.pop();///判断勿忘,返回一个文件夹
51                     }
52                     else if(now == "/")
53                     {
54                         while(!s1.empty()) s1.pop();///重新建立根目录,清空所有
55                     }
56                     else///在当前文件夹里新建文件夹
57                     {
58                         s1.push(now);
59                     }
60                     now = "";///清空勿忘
61                 }///文件夹是我这么说而已,为了便于理解,其实是目录的说....
62             }
63         }
64     }
65     return 0;
66 }
View Code

 

codeforces 158c

标签:

原文地址:http://www.cnblogs.com/VectorLin/p/5479145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!