CodeForces 158C.Cd and pwd command
来源:互联网 发布:it专业课程 编辑:程序博客网 时间:2024/06/05 05:30
题目:http://codeforces.com/problemset/problem/158/C
C. Cd and pwd commands
time limit pertest
memory limit pertest
input
output
Vasya is writing anoperating system shell, and it should have commands for workingwith directories. To begin with, he decided to go with just twocommands:
Directories inVasya's operating system form a traditional hierarchical treestructure. There is a single root directory, denoted by the slashcharacter "/". Every other directory has a name — a non-emptystring consisting of lowercase Latin letters. Each directory(except for the root) has a parent directory — the one thatcontains the given directory. It is denoted as "..".
Thecommand
Thecommand
Initially, thecurrent directory is the root. All directories mentioned explicitlyor passed indirectly within anycommand
Input
The first line of theinput data contains the singleinteger
Thenfollow
The commandparameter
Directories in thefile system can have the same names.
Output
For eachcommand
Sample test(s)
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/
原文:http://www.xuebuyuan.com/576977.html
解题说明:这题模拟了linux下对文件路径的操作,包括.. /这些符号的使用。按照题目要求,肯定是输入一条数据就处理一条数据,处理的意思就是确定当前的路径。一个很容易想到的方法是用树来实现,不过用在这种题目上过于复杂了。这题完全可以用string来模拟,用一个string来保存当前的路径,中间用/隔开,当出现../时回退到上一个/所在处,否则就把读取到的值放入string,可能是覆盖或是拼接。
#include <iostream>#include<string>using namespace std;int main(){string temp,pr,st;//temp用来保存当前截取的字符串,pr用于输出,st用于输入int i,j,n,len;scanf("%d",&n);pr="/";//pr初始化temp="";//temp初始化while (n--){cin>>st;if (st=="cd"){cin>>st;st+='/';len=st.length();for (i=0;i<len;i++){temp+=st[i];//每次截取st子字符串,遇到'/'时停下if (st[i]=='/'){if (temp=="/") //初始化pr为根目录pr=temp;elseif (temp=="../")//返回上级目录{for (j=pr.length()-1;pr[j-1]!='/';j--);pr.resize(j);}elsepr+=temp;temp="";}}}else cout<<pr<<endl;}return 0;}
注意事项:
string是类,不能使用printf输出,printf()只能打印基本类型。
应该用
string s;
cout << s;
或
printf("%s", s.c_str()); //不推荐
- CodeForces 158C.Cd and pwd command
- Codeforces 158C - Cd and pwd commands
- [Codeforces] 158C - Cd and pwd commands
- CodeForces 158C Cd and pwd commands
- codeforces C. Cd and pwd commands 实现命令行
- C. Cd and pwd commands
- C. Cd and pwd commands
- VK Cup 2012 Qualification Round 1--C. Cd and pwd commands
- VK Cup 2012 Qualification Round 1---C. Cd and pwd commands
- Linux--常用命令--cd、pwd
- Linux指令--cd,pwd
- pwd,cd,type
- linux cd pwd 命令
- cd clear ln ls pwd
- linux命令之---cd&pwd
- Linux 常用命令解析 cd pwd
- Nokia N9 常用终端命令(cd,pwd)
- shell 基础 $(cd `dirname $0`;pwd)
- Java实现的Sequence工具 - 2
- SqlHelper类
- Zookeeper的配置
- 数论之因子个数的求法
- 求两个或N个数的最大公约数(gcd)和…
- CodeForces 158C.Cd and pwd command
- struct学习
- Spreadsheet 常用属性
- CS与BS区别
- 怎样在easyUi里面获取到easyui-tree
- Jquery 基础
- CSDN密码又被改了
- 杨辉三角
- Java生成流水号 - 1