csp 201604-3路径解析
csp 201604-3路径解析
一道简单模拟,主要是读入,这题如果会读入就太简单了
用stringstream
path="/d1/d2/d3"
stringstream ss(path);
while(getline(ss,dir,'/'))
的结果就是
“”//第一次为空,因为上来就是‘/’
d1
d2
d3
就是每次读到'/'就停,并且把之前读的放到dir里,dir也是string类型的
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <stack> #include <cstring> #include <sstream> #define inf 2147483647 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(int i=a;i<=b;++i) using namespace std; int T,cnt; stack<string>s,k,temp; string ls,path,dir; void in(int &x){ int y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(int x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } void deal(stack<string> &t,string path){ stringstream ss(path); cnt=0; while(getline(ss,dir,'/')){ ++cnt; if(dir.empty()){ if(cnt==1) while(!t.empty()) t.pop(); } else if(dir==".."){ if(!t.empty()) t.pop(); } else if(dir!=".") t.push(dir); } } void print(){ //p('\n'); while(!k.empty()){ temp.push(k.top()); k.pop(); } if(temp.empty()) p('/'); else{ while(!temp.empty()){ cout<<'/'<<temp.top(); temp.pop(); } } p('\n'); } signed main(){ in(T); getline(cin,ls); deal(s,ls); while(T--){ getline(cin,ls); k=s; deal(k,ls); print(); } return 0; }