C. Cd and pwd commands

8 篇文章 0 订阅

题目链接:http://codeforces.com/problemset/problem/158/C

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 test(s)
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/

#include <stdio.h>
#include <string.h>

char ans[1000][205];//二维数组
char str[10005];//存cd情况下的字符串
char tmp[10005];
int up;

void Print()
{
    int i;
    printf("/");
    for (i=0;i<up;i++)
    {
        printf("%s",ans[i]);
    }
    printf("\n");
}

void Add()
{
  //  printf("%s----------\n",tmp);
    if (tmp[0]=='.')
VK Cup 2012 Qualification Round 1--C. Cd and pwd commands 
    {
        up--;//删除上一行,将未来的新字符串覆盖它
    }
    else
    {
        strcpy(ans[up],tmp);//ans[][]是个二维数组。up会增加,即指针会向下一行移动。
        up++;
    }
}

int main()
{
    int i,j,n,now;
    up=0;
    scanf("%d",&n);
    while(n--)
    {
      //  printf("%d\n",up);
        scanf("%s",str);
        if (str[0]=='p')
        {
            Print();
        }
        else
        {
            scanf("%s",str);
            now=0;
            i=0;
            if (str[0]=='/') {up=0;i=1;}//如果输入的字符串第一个有/,就从下一个字符开头复制字符串,复制到tmp
            for (;i<strlen(str);i++)
            {
                tmp[now++]=str[i];//此时now已经超过str【i】的长度(就输入一个字符哦)
                if (str[i]=='/')//遇到下一个斜杠
                {
                    tmp[now++]='\0';//后面加上结束字符
                    Add();//tmp里面存入的单词或“.”
                    now=0;//归零,便于下一次计数
                }
            }
            tmp[now++]='/';
            tmp[now++]='\0';
            Add();
            now=0;
        }
    }
    return 0;
}

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{//freopen("i.txt","r",stdin);
int n; cin>>n;
string ans[1000],st;
getline(cin,st);
int kol=0;
for (int i=1;i<=n;i++)
 {
   getline(cin,st);
   if (st[0]=='p')
   {cout<<'/';
    for (int i=0;i<kol;i++) cout<<ans[i]<<"/";
    cout<<endl;
   }
   else
   {st+='/';
    int i1,j;
    if (st[3]=='/') {i1=4;kol=0;} else i1=3;
    j=i1;
    for (;i1<st.size();i1++)
    {if (st[i1]=='.') {kol--;i1+=2;j=i1+1;continue;}
     if (st[i1]=='/') {ans[kol]=st.substr(j,i1-j);  kol++; j=i1+1;}
    }
    }
 }
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值