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
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/

解题思路:用字符串操作模拟树。 找绝对路径。

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
using namespace std; int main(){
// freopen("in.txt", "r", stdin);
int n;
string cmd, cur, ans, tt;
while(cin>>n){
ans = "/";
for(int i=0; i<n; i++){
cin>>cmd;
if(cmd == "cd"){
cin>>cur;
cur += '/';
for(int j=0; j<cur.size(); j++){
tt += cur[j];
if(cur[j] == '/'){
if(tt == "/") ans = tt;
else if(tt == "../"){
int k;
for(k=ans.size()-1; ans[k-1]!='/'; k--) ;
ans.resize(k);
}
else ans += tt;
tt = "";
}
}
}
else{
cout<<ans<<endl;
}
}
}
return 0;
}

VK Cup 2012 Qualification Round 1---C. Cd and pwd commands的更多相关文章

  1. VK Cup 2012 Qualification Round 1 C. Cd and pwd commands 模拟

    C. Cd and pwd commands Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  2. DP VK Cup 2012 Qualification Round D. Palindrome pairs

    题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...

  3. VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟

    C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/pr ...

  4. VK Cup 2012 Qualification Round 1 E. Phone Talks —— DP

    题目链接:http://codeforces.com/contest/158/problem/E E. Phone Talks time limit per test 3 seconds memory ...

  5. VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland

    今天在codeforces上面做到一道题:http://codeforces.com/contest/638/problem/B 题目大意是:给定n个字符串,找到最短的字符串S使得n个字符串都是这个字 ...

  6. VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树

    题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...

  7. VK Cup 2016 - Qualification Round 2 D. Three-dimensional Turtle Super Computer 暴力

    D. Three-dimensional Turtle Super Computer 题目连接: http://www.codeforces.com/contest/638/problem/D Des ...

  8. VK Cup 2016 - Qualification Round 2 C. Road Improvement dfs

    C. Road Improvement 题目连接: http://www.codeforces.com/contest/638/problem/C Description In Berland the ...

  9. VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland 水题

    B. Making Genome in Berland 题目连接: http://www.codeforces.com/contest/638/problem/B Description Berlan ...

随机推荐

  1. android蓝牙的调试(博通蓝牙工作 and 低功耗模式)

    首先结合项目从整体上去把握这部分: 蓝牙模块中一个比较核心的文件是bluetooth.c, 在我们上电的时候, 会调用这个文件中bt_enable()这个函数, 在这个函数里面先调用set_bluet ...

  2. java项目打jar包的两种情况

    链接地址:http://jingyan.baidu.com/article/6b97984d8a6ddc1ca2b0bfa0.html 本文介绍一下java项目打jar包时的两种情况各怎么操作   方 ...

  3. InputStream中read()与read(byte[] b)

    原文:InputStream中read()与read(byte[] b) read()与read(byte[] b)这两个方法在抽象类InputStream中前者是作为抽象方法存在的,后者不是,JDK ...

  4. jQuery 简单滑动轮播图效果

    一般页面简单轮播图效果用jQuery制作更加简单.我们来看看以下效果是如何来进行制作的. 其html结构下所示: <div id="box">         < ...

  5. jQuery格式化时间插件formatDate

    一.不传时间 $.formatDate("yyyy-MM-dd HH:mm:ss");   二.传时间 $.formatDate("yyyy-MM-dd HH:mm:ss ...

  6. hdu 2546 饭卡 01背包

    先将前n-1个从小到大排序.对m-5进行01背包.然后答案就是m-dp[m-5]-a[n-1] 至于为什么最后减去最贵的菜品,而不是把最贵的菜品也放到01背包里呢, 由于假设能够把最贵菜品a[n-1] ...

  7. 集合简单总结 ArrayList、List、Hashtable、Dictionary

      ============================ 集合综述 ============================== 1.什么是泛型: 泛型就是限制了操作类型,意思如下:       ...

  8. []: secureCRT连接ubuntu问题- The remote system refused the connection

    secureCRT连接ubuntu问题- The remote system refused the connection http://jxyang.iteye.com/blog/1484915 解 ...

  9. 基于visual Studio2013解决面试题之1309求子集

     题目

  10. uva10791

    #include <iostream> using namespace std; int main(int argc, char *argv[]) { int j,k,m=0,flag; ...