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 命令. 一开始,用户处于根目录 / 下. ...
随机推荐
- 02-vue学习篇-以正确的姿势使用vue
1.渲染数据 #view层 <div class="hello"> <h1>{{ msg }}</h1> //msg </div> ...
- c++入门之文件读取
再次强调这个观念:写文件,读文件和读,写控制台本质上没有区别,意识到这一点是十分重要的.下面给出读文件的代码: #include "iostream" # include &quo ...
- python学习之第八篇——字典嵌套之字典中嵌套字典
cities = { 'shanghai':{'country':'china','population':10000,'fact':'good'}, 'lendon':{'country':'eng ...
- 开发工程中遇到的BUG
Xcode7自带Git创建的项目"Couldn’t communicate with a helper application" git xcode7 zhunjiee 2015年 ...
- rest-framework解析器,url控制,分页,响应器,渲染器,版本控制
解析器 1.json解析器 发一个json格式的post请求.后台打印: request_data---> {'title': '北京折叠'} request.POST---> <Q ...
- [2017BUAA软工助教]个人项目小结
2017BUAA个人项目小结 一.作业链接 http://www.cnblogs.com/jiel/p/7545780.html 二.评分细则 0.注意事项 按时间完成并提交--正常评分 晚交一周以内 ...
- 福州大学软件工程1816 | W班 团队Alpha阶段成绩汇总排名(第9、10次作业)
写在前面 汇总成绩排名链接 1.作业链接 第九次作业--项目Alpha冲刺(团队) 第十次作业--事后诸葛亮(团队) 2.评分准则 本次作业包括现场Alpha答辩评分(映射总分为100分)+博客分(总 ...
- MySQL 批量修改某一列的值为另外一个字段的值
mysql> select * from fruit; +----+--------+-------+ | id | name | price | +----+--------+-------+ ...
- MYSQL 创建数据库SQL
CREATE DATABASE crm CHARACTER SET utf8 COLLATE utf8_general_ci; MySQL :: MySQL 5.7 Reference Manual ...
- js刷新界面前事件onbeforeunload
这个方法的作用是防止填写信息时不小心按了刷新(F5,刷新界面,返回). 目前能实现这个需求的只有这个方法. 具体代码如下: 1.首先在body添加 onbeforeunload 这个事件 <bo ...