codeforces158C
Cd and pwd commands
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
7
pwd
cd /home/vasya
pwd
cd ..
pwd
cd vasya/../petya
pwd
/
/home/vasya/
/home/
/home/petya/
4
cd /a/b
pwd
cd ../a/b
pwd
/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的更多相关文章
- [string]Codeforces158C Cd and pwd commands
题目链接 题意很清楚 和linux的语句是一样的 pwd输出路径 cd进入 ..回上一层目录 此题完全是string的应用 String的用法 vector<string> s; int ...
- CodeForces - 158C(模拟)
题意 https://vjudge.net/problem/CodeForces-158C 你需要实现类似 Unix / Linux 下的 cd 和 pwd 命令. 一开始,用户处于根目录 / 下. ...
随机推荐
- vue编程式导航
vue项目中使用到了组件间传值,通过路由跳转实现从产品页进入产品详情页查看功能. 使用了this.$router.push(编程式导航) product页面中:因为只需要遮住产品列表页来显示产品详情页 ...
- iStack堆叠介绍
iStack堆叠技术简介: 网络中主要存在两种形态的通信设备:盒式设备和框式设备.通常盒式设备部署在网络接入层或对可靠性要求不高的汇聚层,盒式单机设备对端口和带宽扩容不够灵活,扩容增加新的盒式设备 ...
- c++入门之运算符重载
c++函数重载:可以将一个函数名用于不同功能的函数.从而处理不同的对象.对于运算符,同样也有这样的用途,即对同一个标志符的运算符,可以运用到不同的功能中去. 首先引入:运算符重载,在C语言中甚至都有运 ...
- SQLSERVER事务日志已满 the transaction log for database 'xx' is full
解决办法:清除日志 USE [master] GO ALTER DATABASE DNName SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE D ...
- python札记
进制转换 num = "0011"v = int(num, base=16)print(v)2->16
- Mysql MyISAM与InnoDB 表锁行锁以及分库分表优化
一. 两种存储引擎:MyISAM与InnoDB 区别与作用 1. count运算上的区别: 因为MyISAM缓存有表meta-data(行数等),因此在做COUNT(*)时对于一个结构很好的查询是不需 ...
- redis中的hash、列表、集合操作
一.hash操作 数据结构:key:{k1:v1, k2:v2, k3:v3} 类似Python中的字典 如:info : {name: lina, age: 22, sex: F} hset key ...
- jmeter操作数据库
1) jmeter不能直接连数据库,需要先添加jar包. 然后将jar包的路径添加到下图: 2) 操作数据库之前要知道数据库的信息(ip.端口号.账号.密码),操作哪个数据库就连哪个: ...
- Java 读取配置文件数据
Properties类 Properties类,是一个工具类,包含在java.util包中. 功能:可以保存持久的属性,通常用来读取配置文件或者属性文件,将文件中的数据读入properties对象中, ...
- MySQL分页时统计总记录行数并使用limit返回固定数目的记录
需求很简单:假设有一个user表,表中实际上有10000条数据,但是我不知道有多少条,我要从数据库中每次取20条数据显示,那么怎么完成呢? 方案一: 首先执行一个 select count(*) as ...