CodeForces 158C.Cd and pwd command

来源:互联网 发布:it专业课程 编辑:程序博客网 时间:2024/06/05 05:30

题目:http://codeforces.com/problemset/problem/158/C

C. Cd and pwd commands

time limit pertest  3 seconds

memory limit pertest  256 megabytes

input  standardinput

output  standardoutput

 

Vasya is writing anoperating system shell, and it should have commands for workingwith directories. To begin with, he decided to go with just twocommands: cd (change the currentdirectory) and pwd (display thecurrent directory).

Directories inVasya's operating system form a traditional hierarchical treestructure. There is a single root directory, denoted by the slashcharacter "/". Every other directory has a name — a non-emptystring consisting of lowercase Latin letters. Each directory(except for the root) has a parent directory — the one thatcontains the given directory. It is denoted as "..".

Thecommand cd takes a singleparameter, which is a path in the file system. The command changesthe current directory to the directory specified by the path. Thepath consists of the names of directories separated by slashes. Thename of the directory can be "..", which means a step up to theparent directory. «..» can be used in any place of the path, maybeseveral times. If the path begins with a slash, it is considered tobe an absolute path, that is, the directory changes to thespecified one, starting from the root. If the parameter begins witha directory name (or ".."), it is considered to be a relative path,that is, the directory changes to the specified directory, startingfrom the current one.

Thecommand pwd should display theabsolute path to the current directory. This path must not contain"..".

Initially, thecurrent directory is the root. All directories mentioned explicitlyor passed indirectly within anycommand cd are considered toexist. It is guaranteed that there is no attempt of transition tothe parent directory of the root directory.

 

Input

The first line of theinput data contains the singleinteger (1 ≤ n ≤ 50) — thenumber of commands.

Thenfollow lines, each contains onecommand. Each of these lines contains eithercommand pwd, or command cd,followed by a space-separated non-empty parameter.

The commandparameter cd only contains lowercase Latin letters, slashes and dots, two slashes cannot goconsecutively, dots occur only as the name of a parentpseudo-directory. The commandparameter cd does not end with aslash, except when it is the only symbol that points to the rootdirectory. The command parameter has a length from 1 to 200characters, inclusive.

Directories in thefile system can have the same names.

 

Output

For eachcommand pwd you should print thefull absolute path of the given directory, ending with a slash. Itshould start with a slash and contain the list of slash-separateddirectories in the order of being nested from the root to thecurrent folder. It should contain no dots.

 

Sample test(s)

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.xuebuyuan.com/576977.html

解题说明:这题模拟了linux下对文件路径的操作,包括.. /这些符号的使用。按照题目要求,肯定是输入一条数据就处理一条数据,处理的意思就是确定当前的路径。一个很容易想到的方法是用树来实现,不过用在这种题目上过于复杂了。这题完全可以用string来模拟,用一个string来保存当前的路径,中间用/隔开,当出现../时回退到上一个/所在处,否则就把读取到的值放入string,可能是覆盖或是拼接。

 

#include <iostream>#include<string>using namespace std;int main(){string temp,pr,st;//temp用来保存当前截取的字符串,pr用于输出,st用于输入int i,j,n,len;scanf("%d",&n);pr="/";//pr初始化temp="";//temp初始化while (n--){cin>>st;if (st=="cd"){cin>>st;st+='/';len=st.length();for (i=0;i<len;i++){temp+=st[i];//每次截取st子字符串,遇到'/'时停下if (st[i]=='/'){if (temp=="/") //初始化pr为根目录pr=temp;elseif (temp=="../")//返回上级目录{for (j=pr.length()-1;pr[j-1]!='/';j--);pr.resize(j);}elsepr+=temp;temp="";}}}else cout<<pr<<endl;}return 0;}


 

注意事项:

string是类,不能使用printf输出,printf()只能打印基本类型。

应该用

string s;

cout << s;

printf("%s", s.c_str()); //不推荐

 

0 0
原创粉丝点击