Time Limit: 20 Sec
Memory Limit: 256 MB
http://codeforces.com/problemset/problem/158/C
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.
Sample Input
7
pwd
cd /home/vasya
pwd
cd ..
pwd
cd vasya/../petya
pwd
Sample Output
/
/home/vasya/
/home/
/home/petya/
题意
俩操作
cd 改变路径
pwd输出路径
..就是返回到上一级
题解:
模拟一下就好了~
代码
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) #define maxn 1050005 #define mod 10007 #define eps 1e-9 const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************** int n,z; string c,s,p="/"; int main() { cin>>n; for(int i=1;i<=n;i++) { cin>>c; if(c=="cd") { cin>>c; c+="/"; if(c[0]=='/') { p="/"; z=1; } for(int j=z;j<c.size();j++) { if(c[j]=='.'&&c[j+1]=='.'&&c[j+2]=='/') { int k=p.size()-2; p[k+1]='\0'; p.resize(p.size()-1); while(p[k]!='/') { p[k]='\0'; k--; p.resize(p.size()-1); } j+=2; } else { p+=c[j]; } } z=0; } if(c=="pwd") cout<<p<<endl; } }