VK Cup 2012 Qualification Round 1---C. Cd and pwd commands
3 seconds
256 megabytes
standard input
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.
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.
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.
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/
解题思路:用字符串操作模拟树。 找绝对路径。
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的更多相关文章
- 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 ...
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- 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 ...
- 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 ...
- VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland
今天在codeforces上面做到一道题:http://codeforces.com/contest/638/problem/B 题目大意是:给定n个字符串,找到最短的字符串S使得n个字符串都是这个字 ...
- VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树
题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- C# 课堂总结5-数组
一. 数组:解决同一类大量数据在内存存储和运算的功能. 1.一维数组定义:制定类型,指定长度,指定名称.int[] a=new int[5]int[] a=new int[5]{23,23,23,1, ...
- Android项目使用Assets下的文件
Android项目在编译时,Assets下文件不被编译. Assets下的文件除了 html文件可以直接在项目中使用外,其他的文件都需要做处理滴. 在项目中使用方法: 使用流读取. ...
- 【ant项目构建学习点滴】--(3)打包及运行jar文件
<?xml version="1.0" encoding="UTF-8"?> <project default="compile&q ...
- Android:创建文件或文件夹以及获取sd卡根目录
目录结构: 功能,可以根据录入的目录或者文件夹生成相应的文件或者文件夹 首先需要添加一个权限: <uses-permission android:name="android.permi ...
- codeforces 316F3 Suns and Rays
题目在此 找出中有多少个太阳以及每个太阳的散发线段. 算法 原图: 将图"缩小",如果一个白点的四周有黑点,那么把这个白点变成黑点: 将图"放大",即上述&qu ...
- javascript真的是异步的吗?且看setTimeout的实现原理以及setTimeout(0)的使用场景
在今天之前我一直以为setTimeout这个函数是异步的,无意中看到了一篇关于setTimeout的文章.发现自己曾经的认识全是错误的,赶紧总结下. 先看一段代码: var start = new D ...
- Linux安装完Tomcat后无法登陆管理界面
今天在Linux中安装完Tomcat后无法登陆Tomcat的管理界面,也就无法利用Tomcat管理界面来部署项目. 在Windows中一般配置完Tomcat后,只要在[conf]目录下的“tomcat ...
- java环境变量配置问题
你要配置三个环境变量JAVA_HOMECLASSPATHPath你都配置了吗? 系统变量→新建 JAVA_HOME 变量 .变量值填写jdk的安装目录(本人是 E:\Java\jdk1.7.0) 系统 ...
- Eclipse用法和技巧二十一:工程的展示途径
用eclipse阅读代码的时候,有时候代码会变成如图一所示的样子,有时候可能是图二的样子.为什么有时候是图一有时候是图二,笔者也不是很清楚.不过怎么手动设置成图一图二,就是接下来要讲的东西. 1 ...
- javascript笔记整理(对象遍历封装继承)
一.javascript对象遍历 1.javascript属性访问 对象.属性名 对象['属性名'] var obj={name:'chen',red:function(){ alert(1);ret ...