问一道有关Directory Listing的ACM题
题目是这样的:
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]
我编了一个程序,用多组数据测试了都没有错误,但是上传上去老是说WRONG ANSWER,请大家帮我找找是那种情况没考虑吧,小弟谢谢大家了!
以下是我的源程序:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define DIR 1
#define FIL 2
struct tree
{
char name[11];
int size;
int type;
struct tree *child;
struct tree *sibling;
};
struct t
{
struct tree *tre;
struct t *next;
};
int IsDigit(char c)
{
return ('0'<=c&&c<='9');
}
struct tree *CreatTree()
{
struct tree *temp=malloc(sizeof(struct tree));
if(temp==NULL)
{
printf("Out of memory\n");
exit(0);
}
temp->child=temp->sibling=NULL;
temp->size=0;
temp->type=FIL;
temp->name[0]=0;
return temp;
}
struct tree *Read(char *c)
{
char *name;
struct tree *branch=NULL;
while(*c==' ')
scanf("%c",c);
if(*c!=')'&&*c!=EOF&&*c!='\n')
{
branch=CreatTree();
name=branch->name;
if(*c=='*')
branch->type=DIR;
do{
*(name++)=*c;
scanf("%c",c);
}while(*c!=' ');
*name=0;
while(*c==' ')
scanf("%c",c);
while(IsDigit(*c))
{
branch->size=branch->size*10+*c-'0';
scanf("%c",c);
}
}
return branch;
}
void readin(struct tree *branch,char c)
{
while(c==' '||c=='('||c=='\n')
scanf("%c",&c);
branch->child=Read(&c);
branch=branch->child;
while(branch)
{
branch->sibling=Read(&c);
branch=branch->sibling;
}
}
void add(struct tree *head,char c)
{
struct tree *ptr=head;
for(;ptr;ptr=ptr->sibling)
{
if(ptr->type==DIR)
readin(ptr,c);
else ptr->child=NULL;
}
for(ptr=head;ptr;ptr=ptr->sibling)
if(ptr->type==DIR)
add(ptr->child,c);
}
void DisplayTree(struct tree *root,int depth,char *blank)
{
char blankptr[91];
if(depth)
putchar('\n');
if(root)
{
printf("%s",blank);
printf("_%s[%d]",root->name,root->size);
if((root->sibling==NULL&&root->type==DIR)||depth==0)
blank[8*depth]=' ';
strcat(blank," |");
root=root->child;
while(root)
{
DisplayTree(root,depth+1,blank);
blank[8*depth+9]=0;
root=root->sibling;
}
}
}
int calculatesize(struct tree *root)
{
struct tree *temp=root;
int size=0;
if(root==NULL)
return 0;
while(temp)
{
if(temp->type==DIR)
temp->size+=calculatesize(temp->child);
size+=temp->size;
temp=temp->sibling;
}
return size;
}
struct t *CreatT(struct tree *tr)
{
struct t *temp=malloc(sizeof(struct t));
temp->tre=tr;
temp->next=NULL;
return temp;
}
void Dir()
{
char c,*name;
struct tree *t;
struct t *root=CreatT(NULL),*rootptr=root;
c=getchar();
while(c!=EOF)
{
t=CreatTree();
rootptr->next=CreatT(t);
rootptr=rootptr->next;
t->type=DIR;
while(c==' '||c=='\n')
c=getchar();
name=t->name;
do{
*(name++)=c;
c=getchar();
}while(c!=' ');
*name=0;
while(c==' ')
c=getchar();
while(IsDigit(c))
{
t->size=t->size*10+c-'0';
c=getchar();
}
add(t,c);
calculatesize(t);
do{
c=getchar();
}
while(c==' '||c=='\n');
}
for(rootptr=root->next;rootptr;rootptr=rootptr->next)
{
char blank[91]={'|'};
if(rootptr!=root->next)
putchar('\n');
DisplayTree(rootptr->tre,0,blank);
}
putchar('\n');
}
int main()
{
Dir();
return 0;
}
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]
我编了一个程序,用多组数据测试了都没有错误,但是上传上去老是说WRONG ANSWER,请大家帮我找找是那种情况没考虑吧,小弟谢谢大家了!
以下是我的源程序:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define DIR 1
#define FIL 2
struct tree
{
char name[11];
int size;
int type;
struct tree *child;
struct tree *sibling;
};
struct t
{
struct tree *tre;
struct t *next;
};
int IsDigit(char c)
{
return ('0'<=c&&c<='9');
}
struct tree *CreatTree()
{
struct tree *temp=malloc(sizeof(struct tree));
if(temp==NULL)
{
printf("Out of memory\n");
exit(0);
}
temp->child=temp->sibling=NULL;
temp->size=0;
temp->type=FIL;
temp->name[0]=0;
return temp;
}
struct tree *Read(char *c)
{
char *name;
struct tree *branch=NULL;
while(*c==' ')
scanf("%c",c);
if(*c!=')'&&*c!=EOF&&*c!='\n')
{
branch=CreatTree();
name=branch->name;
if(*c=='*')
branch->type=DIR;
do{
*(name++)=*c;
scanf("%c",c);
}while(*c!=' ');
*name=0;
while(*c==' ')
scanf("%c",c);
while(IsDigit(*c))
{
branch->size=branch->size*10+*c-'0';
scanf("%c",c);
}
}
return branch;
}
void readin(struct tree *branch,char c)
{
while(c==' '||c=='('||c=='\n')
scanf("%c",&c);
branch->child=Read(&c);
branch=branch->child;
while(branch)
{
branch->sibling=Read(&c);
branch=branch->sibling;
}
}
void add(struct tree *head,char c)
{
struct tree *ptr=head;
for(;ptr;ptr=ptr->sibling)
{
if(ptr->type==DIR)
readin(ptr,c);
else ptr->child=NULL;
}
for(ptr=head;ptr;ptr=ptr->sibling)
if(ptr->type==DIR)
add(ptr->child,c);
}
void DisplayTree(struct tree *root,int depth,char *blank)
{
char blankptr[91];
if(depth)
putchar('\n');
if(root)
{
printf("%s",blank);
printf("_%s[%d]",root->name,root->size);
if((root->sibling==NULL&&root->type==DIR)||depth==0)
blank[8*depth]=' ';
strcat(blank," |");
root=root->child;
while(root)
{
DisplayTree(root,depth+1,blank);
blank[8*depth+9]=0;
root=root->sibling;
}
}
}
int calculatesize(struct tree *root)
{
struct tree *temp=root;
int size=0;
if(root==NULL)
return 0;
while(temp)
{
if(temp->type==DIR)
temp->size+=calculatesize(temp->child);
size+=temp->size;
temp=temp->sibling;
}
return size;
}
struct t *CreatT(struct tree *tr)
{
struct t *temp=malloc(sizeof(struct t));
temp->tre=tr;
temp->next=NULL;
return temp;
}
void Dir()
{
char c,*name;
struct tree *t;
struct t *root=CreatT(NULL),*rootptr=root;
c=getchar();
while(c!=EOF)
{
t=CreatTree();
rootptr->next=CreatT(t);
rootptr=rootptr->next;
t->type=DIR;
while(c==' '||c=='\n')
c=getchar();
name=t->name;
do{
*(name++)=c;
c=getchar();
}while(c!=' ');
*name=0;
while(c==' ')
c=getchar();
while(IsDigit(c))
{
t->size=t->size*10+c-'0';
c=getchar();
}
add(t,c);
calculatesize(t);
do{
c=getchar();
}
while(c==' '||c=='\n');
}
for(rootptr=root->next;rootptr;rootptr=rootptr->next)
{
char blank[91]={'|'};
if(rootptr!=root->next)
putchar('\n');
DisplayTree(rootptr->tre,0,blank);
}
putchar('\n');
}
int main()
{
Dir();
return 0;
}
...全文
相关推荐
mason-acm-website:Mason ACM 学生分会的网站。 主要用于处理 LAN Party 注册
mason-acm-网站安装说明: 克隆仓库如果您还没有安装 composer,请安装 PHP 依赖项管理器。 打开终端,cd /the/directory/you/clonned/to/mason
数据挖掘方面的资源、期刊、会议的网址集合
《Open Directory Project》下的数据挖掘分类目录提供了一系列相关网站链接。Google合作搜索引擎专为数据挖掘定制,可以帮助用户更有效地找到相关资源。
msacm voice chat perfect_voicechatperfect_
描述中的“Directory: Windows DevelopPlat: Visual Basic”指出这是一个基于Windows平台,使用Visual Basic编程语言的开发项目。
利用Python登录学校OJ爬取AC代码
name__ == "__main__": user_id = input("请输入用户名: ") passwd = getpass.getpass("请输入密码: ") to_dir = 'your_directory_path
常用文件格式大全(文件扩展名的含义).pdf
ACM (Windows System Directory File): 这是Windows操作系统中的一个文件,通常与系统配置或驱动程序相关。4.


