SCUT - 240 - 宝华的文件系统 - 模拟

https://scut.online/p/240

就是要小心绝对路径中也有.和..出现。

#include<bits/stdc++.h>
using namespace std;
#define ll long long

struct path{
    string fullpath;
    void toparent(){
        int n;
        n=fullpath.length();
        for(int i=n-1;i>=0;i--){
            if(fullpath[i]=='/'){
                fullpath=fullpath.substr(0,i);
                if(fullpath=="")
                    fullpath="/";
                return;
            }
        }
    }

    void tonext(string ne){
        if(ne==".")
            return;
        else if(ne==".."){
            toparent();
            return;
        }
        else{
            if(fullpath!="/")
                fullpath+=(string)("/"+ne);
            else{
                fullpath+=(string)(ne);
            }
        }
    }
};

int main(){
    int n;
    while(cin>>n){
        string cd,s;
        path cur;
        cur.fullpath="/";
        while(n--){
            cin>>cd>>s;
            int i=0;
            if(s[0]=='/'){
                cur.fullpath="/";
                i=1;
            }
            {
                s+='/';
                string tt;
                for(;i<s.length();i++){
                    if(s[i]!='/'){
                        tt+=s[i];
                    }
                    else{
                        cur.tonext(tt);
                        tt="";
                    }
                }
            }

        }
        cout<<cur.fullpath<<endl;
    }
}

 

posted @ 2019-02-19 20:46  韵意  阅读(126)  评论(0)    收藏  举报