• VK Cup 2012 Qualification Round 1 C. Cd and pwd commands 模拟


    C. Cd and pwd commands

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/problemset/problem/158/C

    Description

    Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current directory) and pwd (display the current directory).

    Directories in Vasya's operating system form a traditional hierarchical tree structure. There is a single root directory, denoted by the slash character "/". Every other directory has a name — a non-empty string consisting of lowercase Latin letters. Each directory (except for the root) has a parent directory — the one that contains the given directory. It is denoted as "..".

    The command cd takes a single parameter, which is a path in the file system. The command changes the current directory to the directory specified by the path. The path consists of the names of directories separated by slashes. The name of the directory can be "..", which means a step up to the parent directory. «..» can be used in any place of the path, maybe several times. If the path begins with a slash, it is considered to be an absolute path, that is, the directory changes to the specified one, starting from the root. If the parameter begins with a directory name (or ".."), it is considered to be a relative path, that is, the directory changes to the specified directory, starting from the current one.

    The command pwd should display the absolute path to the current directory. This path must not contain "..".

    Initially, the current directory is the root. All directories mentioned explicitly or passed indirectly within any command cd are considered to exist. It is guaranteed that there is no attempt of transition to the parent directory of the root directory.

    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/

    HINT

    题意

    俩操作

    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]='';
                        p.resize(p.size()-1);
                        while(p[k]!='/')
                        {
                            p[k]='';
                            k--;
                            p.resize(p.size()-1);
                        }
                        j+=2;
                    }
                    else
                    {
                        p+=c[j];
                    }
    
                }
                z=0;
            }
            if(c=="pwd")
                cout<<p<<endl;
        }
    
    }
  • 相关阅读:
    TCP的三次握手与四次挥手
    HashMap源代码分析(JDK1.8)
    HashMap实现原理分析(JDK1.8前)
    codebook法分割前景目标
    平均场景法分割前景目标
    边缘梯度方向直方图的构建
    学习opencv 第六章 习题十三
    《学习OpenCV》 第四章 习题六
    《学习Opencv》第五章 习题6
    Opencv实现的简易绘图工具
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4615274.html
Copyright © 2020-2023  润新知