码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces158C

时间:2019-04-03 22:34:07      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:tor   cin   turn   root   parameter   ecif   symbol   several   while   

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(‘\n‘)
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(\n);
}
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

 

 

codeforces158C

标签:tor   cin   turn   root   parameter   ecif   symbol   several   while   

原文地址:https://www.cnblogs.com/gaojunonly1/p/10651588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!