Directory Listing(zoj 1635)

本文介绍了一道关于UNIX目录树的模拟题,题目要求根据给定的目录结构和文件大小,输出格式化的目录树结构,并计算每个目录的总大小。文章提供了一段C++代码实现,包括读取输入、构建目录树、计算目录大小及打印目录树的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Directory Listing


Time Limit: 1 Second      Memory Limit: 32768 KB


Given a tree of UNIX directories and file/directory sizes, you are supposed to list them as a tree with proper indention and sizes.


Input

The input consists of several test cases. Each case consists of several lines which represent the levels of the directory tree. The first line contains the root file/directory. If it is a directory, then its children will be listed in the second line, inside a pair of parentheses. Similarly, if any of its children is a directory, then the contents of that directory will be listed in the next line, inside a pair of parentheses. The format of a file/directory is:

 

name size or *name size

 

where name, the name of the file/directory, is a string of no more than 10 characters; size > 0 is the integer size of the file/directory; * means the name is a directory. It is guaranteed that name will not contain characters '(', ')', '[', ']', and '*'. There are no more than 10 levels for each case, and no more than 10 files/directories on each level.


Output

For each test case, list the tree in the format shown by the sample. Files/directories that are of depth d will have their names indented by 8d spaces. Do NOT print tabs to indent the output. The size of a directory D is the sum of the sizes of all the files/directories in D, plus its own size.


Sample Input

*/usr 1
(*mark 1 *alex 1)
(hw.c 3 *course 1) (hw.c 5)
(aa.txt 12)
*/usr 1
()


Sample Output

|_*/usr[24]
        |_*mark[17]
        |       |_hw.c[3]
        |       |_*course[13]
        |               |_aa.txt[12]
        |_*alex[6]
                |_hw.c[5]
|_*/usr[1]


Author: CHEN, Yue

 


Source: Zhejiang University Local Contest 2003

有点烦的模拟题..

#include<iostream>

using namespace std;

struct

{

    char name[ 15 ];

    int next[ 15 ];

    int num;

    int key;

    void Init(){ num = 0 ;}

}f[ 150 ];

int tn;

void Add(int index,int& pos,char *str)

{

    int i,len=0;

    int z;

    char s[ 200 ];

 

    for(i=pos;str[i] != ' ' && str[i] !=')' ;i++)s[ len++ ] = str[i];

    if( len == 0 )return ;

    s[ len ] = 0;

    f[ index ].next[ f[index].num ] = tn;

    f[ index ].num++;

    strcpy( f[ tn ].name ,s );

    f[ tn ].Init();

    z = 0;

    for(i++;str[i] !=' ' && str[i] != ')';i++)

    {

       z += str[i] - 48;

       z*=10;

    }

    f[ tn ].key = z/10;

    tn++;

    pos = i;

}

void dfs1(int pos)

{

  for(int i=0;i<f[pos].num;i++)

  {

    dfs1(f[pos].next[i]);

  }

  int z = 0;

  for(int i=0;i<f[pos].num;i++)

  {

     z += f[ f[pos].next[i] ].key ;

  }

  f[ pos ].key += z;

}

int len;

void dfs2(int pos,int d,int a[])

{

    if( d  )printf(" ");

    for(int i=1;i<8*d;i++)

    {

       if(a[i])printf("|");

       else printf(" ");

    }

    printf("|_%s[%d]/n",f[pos].name,f[pos].key);

    for(int i=0;i<f[pos].num;i++)

    {

       if( i + 1 != f[pos].num )a[ 8 * (d + 1 )] = 1 ;

       dfs2( f[ pos ].next[ i ],d+1 ,a);

       a[ 8 * (d+1) ] = 0;

    }

}

int main()

{

    char s[ 200 ];

    int pre,tp;

    int i,j;

    int a[ 2500 ];

    while( gets(s) )

    {

       if( s[0] != '(' )

       {

           f[ 0 ].Init();

           tn = 1;

           for(i=0;s[i] != ' ';i++)

              f[ 0 ].name[i] = s[i];

           f[ 0 ].name[i]=0;

           tp = 0;

           for(i++;s[i];i++)

              tp += s[i] - 48,tp*=10;

           f[0].key = tp/10;

           if( s[0] != '*' )

           {

              printf("|_%s[%d]/n",f[0].name,tp/10);

               continue;

           }

            gets(s);

       }

       i = 0;

       pre = tn;

       while( 1 )

       {

           for(j=1;s[j];j++ )

           {

              if( s[j] == ' ' )continue;

              if( s[j] == '(' )continue;

              Add(i,j,s);

              if( s[j] == ')' )

              {

                  i++;

                  while( i <= pre &&  f[i].name[0] != '*' )i++;

              }

           }

           i = pre;

           while( i < tn && f[i].name[0] != '*' )i++;

           if( i == tn )break;

           pre = tn;

           gets(s);

       }

       memset(a,0,sizeof(a) );

       dfs1(0);

       dfs2(0,0,a);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值