• 集训final D STL中string的应用


    #include<iostream>
    #include<string>
    #include<vector>
    #include<cstdio>
    using namespace std;
    
    int main()
    {
        int n;
        while(cin>>n)
        {
            vector<string>s;
            while(!s.empty())
            {
                s.pop_back();
            }
            while(n--)
            {
                string cmd;
                cin>>cmd;
                if(cmd=="cd")
                {
                    string path;
                    cin>>path;
                    int pos=path.find("/");
                    if(pos==0)
                    {
                        while(!s.empty())
                        {
                            s.pop_back();
                        }
                        path.erase(0,1);
                    }
                    pos=path.find("/");
                    while(pos!=string::npos)
                    {
                        if(path.substr(0,pos)=="..")
                        {
                            if(!s.empty())
                            {
                                s.pop_back();
                            }
                        }else
                        {
                            s.push_back(path.substr(0,pos));
                        }
                        path.erase(0,pos+1);
                        pos=path.find("/");
                    }
                    if(path.length()!=0)
                    {
                        if(path=="..")
                        {
                            if(!s.empty())
                            {
                                s.pop_back();
                            }
                        }else
                        {
                            s.push_back(path);
                        }
                    }
                    
                    /*
                    string path;
                    cin>>path;
                    int pos=path.find("/");
                    if(pos!=0)
                    {
                        int pos_next=path.find("/",pos+1);
                        string pname=substr(pos,pos_next-pos-1);
                        if(pname=="..")
                        {
                            if(!s.empty())
                            {
                                s.pop();
                            }
                        }
                        while(s.fornt()!=pname)
                        {
                            
                        }
                    }
                    while(pos!=string::npos)
                    {
                        int pos_next=path.find("/",pos+1);
                        if(pos_next!=string::npos)
                        {
                            string pname=substr(pos,pos_next-pos-1);
                            if(pname=="..")
                            {
                                if(!s.empty())
                                {
                                    s.pop_back();
                                }
                            }
                        }
                    }*/
                }else if(cmd=="pwd")
                {
                    int i;
                    cout<<"/";
                    for(i=0;i<=int(s.size())-1;i++)
                    {
                        cout<<s[i]<<"/";
                    }
                    cout<<endl;
                }
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    comm---两个文件之间的比较
    fgrep---指定的输入文件中的匹配模式的行
    zip---解压缩文件
    unzip---解压缩“.zip”压缩包。
    tar---打包,解压缩linux的文件和目录
    scp---远程拷贝文件
    make---GNU编译工具
    gcc---C/C++ 编译器
    expr---计算工具
    curl -w 支持的参数
  • 原文地址:https://www.cnblogs.com/cj695/p/2623283.html
Copyright © 2020-2023  润新知