Cd and pwd commands

CodeForces - 158C

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.

Examples

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/ sol:小模拟题,题意略微毒瘤(令人挠头)
题意
读入一个字符串
'/'开头的话,删掉所有文件
字符串S开头,后面跟一个'/',表示在当前目录新建一个叫S的文件,并把这个目录中另一个删掉
'..',清空当前目录,退到上一个目录 sol:容易发现这些操作很像一个栈的出栈入栈操作,于是开个栈模拟一下就好了
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
int n,Top=,Now=;
string Stack[],S;
inline void Print()
{
int i;
putchar('/');
for(i=;i<=Top;i++) cout<<Stack[i];
putchar('\n');
}
int main()
{
int i;
R(n);
while(n--)
{
cin>>S;
if(S=="pwd") Print();
else
{
cin>>S;
string tmp;
int Len=S.size();
S[Len++]='/';
for(i=;i<Len;i++)
{
tmp+=S[i];
if(S[i]=='/')
{
if(tmp=="../")
{
if(Top) Top--;
}
else if(tmp=="/")
{
Top=;
}
else
{
Stack[++Top]=tmp;
}
tmp="";
}
}
}
}
return ;
}
/*
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/
*/
 

codeforces158C的更多相关文章

  1. [string]Codeforces158C Cd and pwd commands

    题目链接 题意很清楚 和linux的语句是一样的 pwd输出路径 cd进入 ..回上一层目录 此题完全是string的应用 String的用法 vector<string> s; int ...

  2. CodeForces - 158C(模拟)

    题意 https://vjudge.net/problem/CodeForces-158C 你需要实现类似 Unix / Linux 下的 cd 和 pwd 命令. 一开始,用户处于根目录 / 下. ...

随机推荐

  1. 【转】DataTable与实体类互相转换

    原文地址:https://www.cnblogs.com/marblemm/p/7084797.html /// <summary> /// DataTable与实体类互相转换 /// & ...

  2. 支持异步同步的分布式CommandBus MSMQ实现 - 支持Session传递、多实例处理

    先上一张本文所描述的适用场景图 分布式场景,共3台server: 前端Server Order App Server Warehouse App Server 功能: 前端Server可以不停的发送C ...

  3. Python全栈开发之路 【第二篇】:Python基础之数据类型

    本节内容 一.字符串 记住: 有序类型:列表,元组,字符串 ---> 都可迭代: 无序类型:字典,集合 ---> 不可迭代: 特性:不可修改 class str(object): &quo ...

  4. nginx负载均衡精简配置实例

    [root@localhost ~]# vim nginx.conf user nginx; worker_processes ; error_log /var/log/nginx/error.log ...

  5. Mysql安装(Ubuntu)

    卸载方法一: --删除mysql的数据文件 sudo rm /var/lib/MySQL/ -R --删除mysql的配置文件 sudo rm /etc/mysql/ -R --自动卸载mysql(包 ...

  6. C++常用代码优化策略

    C++代码常用的优化策略 1.不存在指向空值的引用,意味着引用比指针的效率更高,因为在使用引用之前不需要测试它的合法性:指针可以被重新赋值以指向另一个不同的对象,但是引用总是指向它初始化时指定的对象. ...

  7. sczd

  8. img :src=“” url()

    <img :src="logoImg"> this.logoImg='/static/images/'+adminUser.Logo; v-bind:style=&qu ...

  9. Mysql 中的MVCC原理,undo日志的依赖

    一. MVCC 原理了解   原文点击:MVCC原理浅析 读锁: 也叫共享锁.S锁,若事务T对数据对象A加上S锁,则事务T可以读A但不能修改A,其他事务只能再对A加S锁,而不能加X锁,直到T释放A上的 ...

  10. PHP之常用设计模式

    在日常开放中,经常会用到一些设计模式,进行我们代码的优化处理,一个很好的设计思想 1) 工厂模式 在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象 ...