题目来源:http://poj.org/problem?id=1057

题目大意:计算机的用户通常希望能够看到计算机存储的文件的层次结构的图形化表示。Microsoft Windows的 "Explorer"(文件浏览器)就是这样的应用。在图形界面出现以前,最好的描述文件层级结构的方法是展示一个目录和文件的“map”,来表示文件的目录结构。例如:

ROOT

| DIR1

| File1

| File2

| File3

| DIR2

| DIR3

| File1

File1

File2

上面的实例展示了一个根目录,包含了两个文件和三个子目录,第一个子目录下包含三个文件。子二个子目录为空,第三个子目录含一个文件。

输入:编写一个程序,读入一系列的数据用于表示一个计算机中的文件结构。每个数据集用一个*号来表示结束。整个数据的结束标记为#。数据集中含多个文件和目录。约定文件结构的起始点是root,每个目录的结尾用']'表示。目录名以'd'开头,文件名以'f'开头。文件名可能有扩展名也可能没有扩展名。文件名和目录名不含空格。

输出:对于每个目录先输出其子目录,然后输出包含的文件,文件按字典序输出,每个数据集首先输出一行"DATA SET x:" ,x为数据集序号,从1开始计。每个测试用例之间用空行隔开。每个层级输出一个‘|’后接5个空格。具体见sample。


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

本题的输出格式要求比较严,要特别小心,另外对于每个目录,文件需要排序,但是对于子目录只需要按出现的顺序输出。

可以递归实现,也可以用栈。代码里用的递归。

 ////////////////////////////////////////////////////////////////////
// POJ1057 FILE MAPPING
// Memory: 200K Time: 16MS
// Language: C++ Result : Accepted
//////////////////////////////////////////////////////////////////// #include <iostream>
#include <string>
#include <list> using namespace std;
string str; void HandleDir(int layer, string dir_name) {
list<string> file_list; for (int i = ; i < layer; ++i) {
cout << "| ";
}
cout << dir_name << endl;
while (str != "*" && str != "]") {
if (str[] == 'f') {
file_list.push_back(str);
}
else if (str[] == 'd') {
string dir = str;
cin >> str;
HandleDir(layer + , dir);
}
cin >> str;
}
file_list.sort();
for (list<string>::iterator it = file_list.begin(); it != file_list.end(); ++it) {
for (int i = ; i < layer; ++i) {
cout << "| ";
}
cout << *it << endl;
}
} int main(void) { for (int case_id = ; cin >> str, str != "#"; ++case_id) {
cout << "DATA SET " << case_id << ":" << endl;
HandleDir(, "ROOT");
cout << endl;
}
return ;
}

POJ1057 FILE MAPPING的更多相关文章

  1. File mapping

    文件映射的三个功能: 1.File mapping allows the process to use both random input and output (I/O) and sequentia ...

  2. POJ 1057 File Mapping 最详细的解题报告

    题目来源:POJ 1057 File Mapping 题目大意:像我的电脑那样显示文件夹和文件信息,其中在同一级目录内,文件夹排在文件的前面并且文件夹的顺序不变,同一级目录中文件按字母序排列.文件以‘ ...

  3. OpenJudge 2775 文件结构“图”/ Poj 1057 FILE MAPPING

    1.链接地址: http://bailian.openjudge.cn/practice/2775 http://poj.org/problem?id=1057 2.题目: 总时间限制: 1000ms ...

  4. Creating a File Mapping Object

    创建一个文件映射对象 映射一个文件的第一步是通过调用CreateFile函数来打开一个文件.为了确保其他的进程不能对文件已经被映射的那一部分进行写操作,你应该以唯一访问(exclusive acces ...

  5. Delphi Memory-Mapped File简单示例

    { Copyright ?1999 by Delphi 5 Developer's Guide - Xavier Pacheco and Steve Teixeira } unit MainFrm; ...

  6. Analysis about different methods for reading and writing file in Java language

    referee:Java Programming Tutorial Advanced Input & Output (I/O) JDK 1.4+ introduced the so-calle ...

  7. File System Programming---(三)

    Accessing Files and Directories Before you can open a file, you first have to locate it in the file ...

  8. The Portable Executable File Format from Top to Bottom(每个结构体都非常清楚)

    The Portable Executable File Format from Top to Bottom Randy KathMicrosoft Developer Network Technol ...

  9. Windows API Hooking in Python

    catalogue . 相关基础知识 . Deviare API Hook Overview . 使用ctypes调用Windows API . pydbg . winappdbg . dll inj ...

随机推荐

  1. numpy.percentile

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.percentile.html numpy.percentile(a, q, axi ...

  2. eclipse利用mybatis-generator生成代码

    由于mybatis是半自动的ORM框架,表到POJO的映射可以由mybatis-generator完成,映射文件也可以由它生成,下面介绍生成步骤: 1.新建maven项目:File->Other ...

  3. python爬虫(2)--Urllib库的高级用法

    1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. 拆分这些请求,我们只 ...

  4. JVM实用参数(一)JVM类型以及编译器模式

    JVM实用参数(一)JVM类型以及编译器模式 原文地址:https://blog.codecentric.de/en/2012/07/useful-jvm-flags-part-1-jvm-types ...

  5. a标签中href=""的几种用法(转)

    a标签中href=""的几种用法   标签: html / a标签 / javascript 46371 众所周知,a标签的最重要功能是实现超链接和锚点.而且,大多数人认为a标签最 ...

  6. JS的Prototype属性

    转载至: http://blog.sina.com.cn/s/blog_7045cb9e0100rtoh.html 函数:原型 每一个构造函数都有一个属性叫做原型(prototype,下面都不再翻译, ...

  7. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

    1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...

  8. wordCount总结

    1.github地址:https://github.com/husterSyy/SoftTest 2.PSP表格   psp 2.1 psp阶段 预估耗时(分钟) 实际耗时(分钟) Planning ...

  9. [学习笔记]fork深入理解

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include <unistd.h> ...

  10. 基于jQuery的Tooltip

    近来,要开发一个关务管理系统,为了增加系统美观度,自己开发了一个基于jQuery框的的小插件,与大家分享一下,希望大家能给我提出宝贵修改意见. 下面开发说明使用方法和内容: 一.引用jQuery框架, ...