Directory Listing(zoj 1635)

来源:互联网 发布:珠海博明视觉 知乎 编辑:程序博客网 时间:2024/04/29 12:05

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);

    }

}

原创粉丝点击