POJ 1057

题意:模拟数据库系统,给你文件及其对应关系,画出图形。以f开头的是文件,以d开头的是文件夹,']'代表文件夹结束,'*'代表样例结束,'#'代表输入结束。

题解:模拟。

View Code
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<string>
 6 using namespace std;
 7 const int N=10000;
 8 struct data
 9 {
10     char name[100];
11     vector<string> file;
12     vector<int> son;
13     int level;
14     void init(const char * s,int _le)
15     {
16         strcpy(name,s);
17         level=_le;
18         file.clear();
19         son.clear();
20     }
21 } po[N];
22 int dep[N];
23 void dfs(int now)
24 {
25     char st[]={"|     "},tep[1000];
26     tep[0]='\0';
27     int d=po[now].level;
28     while(d--)
29         strcat(tep,st);
30     printf("%s",tep);
31     printf("%s\n",po[now].name);
32     d=po[now].son.size();
33     for(int i=0;i<d;i++)
34         dfs(po[now].son[i]);
35     sort(po[now].file.begin(),po[now].file.end());
36     d=po[now].file.size();
37     for(int i=0;i<d;i++)
38         printf("%s%s\n",tep,po[now].file[i].c_str());
39 }
40 int main()
41 {
42     int ca=0;
43     char s[100];
44     bool flag=true;
45     while(gets(s),strcmp(s,"#")!=0)
46     {
47         if(flag)
48             flag=false;
49         else
50             printf("\n");
51         printf("DATA SET %d:\n",++ca);
52         if(strcmp(s,"*")==0)
53         {
54             printf("ROOT\n");
55             continue;
56         }
57         int depth=0,n=1;
58         memset(dep,0,sizeof(dep));
59         po[0].init("ROOT",0);
60         if(s[0]=='f')
61             po[0].file.push_back(s);
62         else
63         {
64             po[n].init(s,++depth);
65             po[0].son.push_back(n);
66             dep[depth]=n++;
67         }
68         while(gets(s),strcmp(s,"*")!=0)
69         {
70             if(s[0]=='f')
71                 po[dep[depth]].file.push_back(s);
72             else if(s[0]=='d')
73             {
74                 po[dep[depth]].son.push_back(n);
75                 po[n].init(s,++depth);
76                 dep[depth]=n++;
77             }
78             else
79             {
80                 depth--;
81             }
82         }
83         dfs(0);
84     }
85     return 0;
86 }

转载于:https://www.cnblogs.com/tmeteorj/archive/2012/10/08/2715679.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值