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

时间:2022-08-27 00:02:02
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&period; 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&period; Palindrome pairs

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

  3. VK Cup 2012 Qualification Round 2 C&period; String Manipulation 1&period;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&period; 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&period; Making Genome in Berland

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

  6. VK Cup 2015 - Qualification Round 1 D&period; Closest Equals 离线&plus;线段树

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

  7. VK Cup 2016 - Qualification Round 2 D&period; 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&period; 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&period; Making Genome in Berland 水题

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

随机推荐

  1. Learn ZYNQ&lpar;10&rpar; &ndash&semi; zybo cluster word count

    1.配置环境说明 spark:5台zybo板,192.168.1.1master,其它4台为slave hadoop:192.168.1.1(外接SanDisk ) 2.单节点hadoop测试: 如果 ...

  2. angularJs之service

    自定义服务: 方法一:controller中返回值,service中return <!DOCTYPE html> <html> <head> <meta ch ...

  3. 今天自己解决了两个问题(IE10 type slow ChromeDriver erro)

    都是通过google解决的,其实本应该很快解决,可是因自己粗心,大写小错了,加上java基础不过关, "webdriver.chrome.driver"中的webdriver应是全 ...

  4. Airline Hub

    参考:http://blog.csdn.net/mobius_strip/article/details/12731459 #include <stdio.h> #include < ...

  5. OpenGL立方体在世界坐标系中&lowbar;缩放&lowbar;旋转&lowbar;平移&lowbar;顶点片源着色器&lowbar;光照作用&lowbar;棋盘纹理贴图

    读取bmp等图片格式中的像素还有难度,就先用这个棋盘图象素来弄了 代码打错一个就一直First-chance exception ,貌似还有一个要用q或者Q才能成功退出,不知道缺少哪句,我用窗口红叉退 ...

  6. oracle 查看表的定义

    select t.table_name 表名, c.comments 字段名称, t.column_name 字段编码, t.data_type || '(' || to_char(t.data_le ...

  7. php之图片处理类缩略图加水印

    用到两个image系统函数 imagecopymerge — 拷贝并合并图像的一部分 imagecopyresampled — 重采样拷贝部分图像并调整大小 /* 如何知道图片的大小和类型 无法确认调 ...

  8. 查看Linux下网卡状态或 是否连接&lpar;转&rpar;

      1) 通过mii-tool指令       [root@localhost root]# mii-tool        eth0: negotiated 100baseTx-FD, link o ...

  9. uploadify&period;js

    基于uploadify.js实现多文件上传和上传进度条的显示 uploadify是JQuery的一个插件,主要实现文件的异步上传功能,可以自定义文件大小限制.文件类型.是否自动上传等属性,可以显示上传 ...

  10. 目标检测网络之 YOLOv2

    YOLOv1基本思想 YOLO将输入图像分成SxS个格子,若某个物体 Ground truth 的中心位置的坐标落入到某个格子,那么这个格子就负责检测出这个物体. 每个格子预测B个bounding b ...