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 ...
随机推荐
- Animation 的setFillAfter
疑问:如果图片从其他地方回到原先位置,则setFillAfter(false)也可以保持图片在原先位置?
- android一个页面上多个listview
android一个页面上多个listview,在滚动的时候,需要两个listview能够一起滚动,看起来是一个view. 这个功能的具体实现,参考: http://blog.csdn.net/xia2 ...
- ZJOI2013 防守战线
题目 战线可以看作一个长度为\(n\)的序列,现在需要在这个序列上建塔来防守敌兵,在序列第\(i\)号位置上建一座塔有\(C_i\)的花费,且一个位置可以建任意多的塔,费用累加计算.有\(m\)个区间 ...
- Ubuntu一些配置和技巧
安装google-chrome wget https://dl.google.com/linux/direct/google-chrome-stable_current_i386.deb sudo d ...
- 业余写的一个播放器SDK,求点意见
好久没写博客了 现大致花了半年时间私下写一个音频SDK,想请csdn的达人提点意见,看看还需要增加哪些功能 我对这个的定位如下: 可以在游戏开发中播放音乐,作为一般的音频播放器后端,作为音频编辑器后端 ...
- C#:总结页面传值几种方法
小知识点: 1. W7自带 .NetFrameWork 3.5, 兼容模式为 高版本号兼容低版本号: 2. WF和WPF都是基于XAML的,可是两者的用途不同. WF是一种开发框架,将工作流嵌入在. ...
- Controller@实现Controller的两种形式
实现Controller的两种形式 形式1:仅仅实现IController接口,自定义Controller对Request的实现.形式2:在实现IController接口以后,继承Controller ...
- @Autowired 注释与@Qualifier 注释
@Service("OrganDaoIbatis") public class OrganDaoIbatis extends BaseDao implements IOrganDa ...
- SRM589 DV1 250 回文字符串
其实这道题挺简单的,不过刚开始我钻了一个错误的死胡同.想明白之后才发现. 题目要求用最少的时间来将一个字符串变成回文字符串.(具体题目参看topcoder srm589 DV1 250分值的题目,不便 ...
- MSSQL - 因为数据库正在使用,所以无法获得对数据库的独占访问权。
关于“因为数据库正在使用,所以无法获得对数据库的独占访问权”的最终解决方案 今天在使用SQL Server2005对某个数据库进行还原操作的时候,出现了如上问题,“因为数据库正在使用,所以无法获得 ...