zoukankan      html  css  js  c++  java
  • codeforces158C

    Cd and pwd commands

     CodeForces - 158C 

    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.

    Examples

    Input
    7
    pwd
    cd /home/vasya
    pwd
    cd ..
    pwd
    cd vasya/../petya
    pwd
    Output
    /
    /home/vasya/
    /home/
    /home/petya/
    Input
    4
    cd /a/b
    pwd
    cd ../a/b
    pwd
    Output
    /a/b/
    /a/a/b/

    sol:小模拟题,题意略微毒瘤(令人挠头)
    题意
    读入一个字符串
    '/'开头的话,删掉所有文件
    字符串S开头,后面跟一个'/',表示在当前目录新建一个叫S的文件,并把这个目录中另一个删掉
    '..',清空当前目录,退到上一个目录

    sol:容易发现这些操作很像一个栈的出栈入栈操作,于是开个栈模拟一下就好了
    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0'); return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    int n,Top=0,Now=0;
    string Stack[10005],S;
    inline void Print()
    {
        int i;
        putchar('/');
        for(i=1;i<=Top;i++) cout<<Stack[i];
        putchar('
    ');
    }
    int main()
    {
        int i;
        R(n);
        while(n--)
        {
            cin>>S;
            if(S=="pwd") Print();
            else
            {
                cin>>S;
                string tmp;
                int Len=S.size();
                S[Len++]='/';
                for(i=0;i<Len;i++)
                {
                    tmp+=S[i];
                    if(S[i]=='/')
                    {
                        if(tmp=="../")
                        {
                            if(Top) Top--;
                        }
                        else if(tmp=="/")
                        {
                            Top=0;
                        }
                        else
                        {
                            Stack[++Top]=tmp;
                        }
                        tmp="";
                    }
                }
            }
        }
        return 0;
    }
    /*
    input
    7
    pwd
    cd /home/vasya
    pwd
    cd ..
    pwd
    cd vasya/../petya
    pwd
    output
    /
    /home/vasya/
    /home/
    /home/petya/
    
    input
    4
    cd /a/b
    pwd
    cd ../a/b
    pwd
    output
    /a/b/
    /a/a/b/
    */
    View Code
     
  • 相关阅读:
    对象数组、集合、链表(java基础知识十五)
    正则表达式、Calendar类、SimpleDateFormat类、Date类、BigDecimal类、BigInteger类、System类、Random类、Math类(Java基础知识十四)
    stringBuffer、StringBuilder、排序、Arrays、Jdk1.5新特性(java基础知识十三)
    Scanner、String(java基础知识十二)
    开发工具、Object类(java基础知识十一)
    包、修饰符、内部类、匿名内部类(java基础知识十)
    获取cookies的简单代码(总结待续)
    多态、抽象类、接口、区别(java基础知识九)
    代码块、继承、this、super、final(java基础知识八)
    构造方法,重载,static,math类(java基础知识七)
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10651588.html
Copyright © 2011-2022 走看看