【poj1057】FILE MAPPING 模拟

Description

It is often helpful for computer users to see a visual representation of the file structure on their computers. The “explorer” in Microsoft Windows is an example of such a system. Before the days of graphical user interfaces, however, such visual representations were not possible. The best that could be done was to show a static “map”of directories and files, using indentation as a guide to directory contents. For example:

ROOT

|   DIR1

|        File1

|   File2

|   File3

|   DIR2

|   DIR3

|   File1

File1

File2

This shows that the root directory contains two files and three subdirectories. The first subdirectory contains 3 files, the second is empty and the third contains one file.

Input

Write a program that reads a series of data sets representing a computer file structure. A data set ends with a line containing a single *, and the end of valid data is denoted by a line containing a single #. The data set contains a series of file and directory names. (The root directory is assumed to be the starting point.) The end of a directory is denoted by a ‘]’ Directory names begin with a lower case ‘d’ and file names begin with a lower case ‘f’ File names may or may not have an extension (such as fmyfile.dat or fmyfile). File and directory names may not contain spaces.

Output

Note that the contents of any directory should list any subdirectories first, followed by files, if any. All files should be in alphabetical order within each directory. Note that each data set output is marked by the label “DATA SET x:” where x denotes the number of the set, beginning at 1. Note also the blank line between the output data sets. Each level of indentation should show a ‘|’ followed by 5 spaces.

Sample Input

file1
file2
dir3
dir2
file1
file2
]
]
file4
dir1
]
file3
*
file2
file1
*
#

Sample Output

DATA SET 1:
ROOT
|     dir3
|     |     dir2
|     |     file1
|     |     file2
|     dir1
file1
file2
file3
file4

DATA SET 2:
ROOT
file1
file2

Source

Pacific Northwest 1998


题意简单明了,看样例

还蛮好写的…几乎没调试

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

typedef long long LL;
const int SZ = 1010;
const int INF = 1000000010;

struct node{
    int fcnt,dcnt;
    string f[30],d[30],name;
    node *dir[30],*fa;
}T[SZ], *root;

int Tcnt = 0;

node* newnode(string s,node *fa)
{
    node *k = T + (Tcnt ++);
    memset(k -> dir,0,sizeof(k -> dir));
    k -> fa = fa;
    for(int i = 0;i < 30;i ++) k -> f[i] = k -> d[i] = "";
    k -> fcnt = k -> dcnt = 0;
    k -> name = s;
    return k; 
}

void printtab(int x)
{
    for(int i = 1;i <= x;i ++)
        printf("|     ");
}

void dfs(node *p,int d)
{
    if(!p) return;
    cout << p -> name << endl;
    for(int i = 1;i <= p -> dcnt;i ++)
        printtab(d + 1),dfs(p -> dir[i],d + 1);
    sort(p -> f + 1,p -> f + 1 + p -> fcnt);
    for(int i = 1;i <= p -> fcnt;i ++)
        printtab(d),cout << p -> f[i] << endl; 
}

void init() { Tcnt = 0; root = newnode("ROOT",NULL); }

string s[SZ];

int main()
{
    init();
    int Case = 0,n = 1;
    while(cin >> s[n]) n ++;


    node *p = root;
    for(int i = 1;i <= n;i ++)
    {
        if(s[i][0] == 'f')
            p -> f[++ p -> fcnt] = s[i];
        else if(s[i][0] == 'd')
        {
            p -> d[++ p -> dcnt] = s[i];
            p -> dir[p -> dcnt] = newnode(s[i],p);
            p = p -> dir[p -> dcnt];
        }
        else if(s[i][0] == ']')
            p = p -> fa;
        else if(s[i] == "*")
        {
            printf("DATA SET %d:\n",++ Case);
            dfs(root,0); puts("");
            p = root;
            init();
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值