题目来源: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. linux命令-mke2fs

    想在磁盘下写东西,必须要先格式化 /////////////////////////////////////////////////////////////////////////////////// ...

  2. Eclipse Maven 编译错误 Dynamic Web Module 3.0 requires Java 1.6 or newer 解决方案

    Eclipse Maven 开发一个 jee 项目时,编译时遇到以下错误:Description Resource Path Location TypeDynamic Web Module 3.0 r ...

  3. Mac系统下MySql下载MySQL5.7及详细安装流程

    一.在浏览器当中输入以下地址 https://dev.mysql.com/downloads/mysql/    二.进入以下界面:直接点击下面位置 ,选择跳过登录 点过这后直接下载. 三.下载完成后 ...

  4. plupload的一些使用心得

    最近要做一个文件上传的东西 经过同事的推荐所以就选择了plupload,挺强大的 由于项目框架为改动后的MVC 刚一开始破费周折 不过最后总算是完成了 废话不多说了 粘出来代码给大家参考吧!文件包大家 ...

  5. JavaPersistenceWithHibernate第二版笔记-第七章-001Mapping a set(@ElementCollection、@CollectionTable、@JoinColumn、)

    一.结构 二.代码 1. package org.jpwh.model.collections.setofstrings; import org.jpwh.model.Constants; impor ...

  6. ZROI2018提高day9t1

    传送门 分析 我们首先想到的自然是根据大小关系建图,在这之后我们跑一遍拓扑排序 但是由于l和r的限制关系我们需要对传统的拓扑排序做一些改变 我们考虑将所有入度为0且现在的拓扑序号已经大于等于l的点放入 ...

  7. Json Post到 https的坑 - the underlying connection was closed an unexpected error occurred on a send(远程服务器未知错误导致关闭)

    最近做了一个安装包,安装包会弹出dotnet的 窗体,这个安装包会去调用https的一个api.用测试程序测试窗体都是好的.一旦打入安装包后,就报错.研究了半天,原来是https惹的祸 解决方案: . ...

  8. spring项目中监听器作用-ContextLoaderListener

    附加链接:http://blog.csdn.net/zjw10wei321/article/details/40145241 作用:在启动Web 容器时,自动装配Spring applicationC ...

  9. redis系列:哨兵

    1 简介 Sentinel(哨兵)是Redis 的高可用性解决方案:通过哨兵可以创建一个当主服务器出现故障时自动将从服务器升级为主服务器的一个分布式系统.解决了主从复制出现故障时需要人为干预的问题. ...

  10. xcodebuild命令

    一.xcodebuild命令介绍 1.基本命令 查看xcodebuild简洁用法 xcodebuild -usage 查看已安装的SDK xcodebuild -showsdks 查看安装的版本号 x ...