2018_2_13_Directory Listing_树_链表

10 篇文章 0 订阅
Directory Listing

Time Limit: 2 Seconds      Memory Limit: 65536 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>
#include<cstdio>
#include<algorithm>
#include<queue> 

using namespace std;

struct AA{
	char b[11];
	int add,next[10],num;
}a[110];

int idx;
bool w[100];
queue<int> q;

void input(int x){
	char ch=getchar();
	while(ch!='(')ch=getchar();
	ch=getchar();
	while(ch!=')'){
		int i=0;
		while(ch!=' '){
			a[idx].b[i++]=ch;
			ch=getchar();
		}
		if(a[idx].b[0]=='*')q.push(idx);
		a[idx].b[i]=0;
		a[x].next[a[x].num++]=idx;
		scanf("%d",&a[idx].add);
		a[idx++].num=0;
		ch=getchar();
		while(ch==' ')ch=getchar();
	}
}

int sum(int x){
	for(int i=0;i<a[x].num;i++)
	a[x].add+=sum(a[x].next[i]);
	return a[x].add;
}

void output(int x,int y){
	for(int i=0;i<y*8;i++){
		if(w[i])putchar('|');
		else putchar(' ');
	}
	printf("|_%s[%d]\n",a[x].b,a[x].add);
	for(int i=0;i<a[x].num;i++){
		if(i+1==a[x].num)w[(y+1)*8]=0;
		else w[(y+1)*8]=1;
		output(a[x].next[i],y+1);
	}
}


int main(){
	while(~scanf("%s%d",a[0].b,&a[0].add)){
		if(a[0].b[0]=='*')q.push(0);
		idx=1;
		a[0].num=0;
		while(!q.empty()){
			int t=q.front();
			q.pop();
			input(t);
		}
		sum(0);
		output(0,0);
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值