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;
    }
}

 

相关文章:

  • 2021-10-29
  • 2021-04-14
猜你喜欢
  • 2022-01-24
  • 2021-05-19
  • 2021-07-20
  • 2021-05-30
  • 2021-06-12
  • 2021-06-02
  • 2021-05-19
相关资源
相似解决方案