POJ 1057 File Mapping 最详细的解题报告
题目来源:POJ 1057 File Mapping
题目大意:像我的电脑那样显示文件夹和文件信息,其中在同一级目录内,文件夹排在文件的前面并且文件夹的顺序不变,同一级目录中文件按字母序排列。文件以‘f’开头,文件夹以‘d’开头,‘*’表示一个case的结束,‘#’表示所有输入内容结束。
解题思路:递归创建,最后递归输出。算法比较简单,我就不细说了,具体看代码:
具体算法(java版,可以直接AC)
1 import java.util.*; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 Node root = null; 7 Node parent = null; 8 String line = ""; 9 int count = 1; 10 do { 11 root = new DirectoryNode("ROOT", null); 12 parent = root; 13 do { 14 line = input.next(); 15 if (line != null && !line.isEmpty()) { 16 line = line.trim(); 17 Node childNode=null; 18 if (line.startsWith("f")) { 19 childNode=new FileNode(line,parent); 20 parent.children.add(childNode); 21 }else if (line.startsWith("d")) { 22 childNode=new DirectoryNode(line,parent); 23 parent.children.add(childNode); 24 parent=childNode; 25 }else if (line.startsWith("]")) { 26 if(parent!=null){ 27 parent=parent.parent; 28 } 29 } 30 } 31 } while (!line.startsWith("*")); 32 System.out.println(String.format("DATA SET %s:", count++)); 33 root.output(); 34 if(!line.startsWith("#")){ 35 System.out.println(); 36 } 37 } while (!line.startsWith("#")); 38 } 39 } 40 41 abstract class Node { 42 public String name; 43 public Node parent; 44 public String prefix; 45 public List<Node> children; 46 47 public Node(String name, Node parent) { 48 this.name = name; 49 this.parent = parent; 50 this.prefix = ""; 51 if (this.parent != null) { 52 this.prefix = this.parent.prefix + this.prefix; 53 } 54 this.children = new ArrayList<Node>(); 55 } 56 57 public void sort() { 58 Collections.sort(this.children, new Comparator<Node>() { 59 public int compare(Node left, Node right) { 60 if (left instanceof DirectoryNode) { 61 return -1; 62 }else{ 63 if (right instanceof FileNode){ 64 return left.name.compareTo(right.name); 65 }else{ 66 return 1; 67 } 68 } 69 } 70 }); 71 } 72 73 public abstract void output(); 74 } 75 76 class FileNode extends Node { 77 78 public FileNode(String name, Node parent) { 79 super(name, parent); 80 } 81 82 @Override 83 public void output() { 84 if (this.parent != null) { 85 this.prefix = this.parent.prefix + this.prefix; 86 } 87 System.out.println(this.prefix + this.name); 88 } 89 } 90 91 class DirectoryNode extends Node { 92 93 public DirectoryNode(String name, Node parent) { 94 super(name, parent); 95 } 96 97 @Override 98 public void output() { 99 if (this.parent != null) { 100 this.prefix = this.parent.prefix + "| "; 101 } 102 System.out.println(this.prefix + this.name); 103 this.sort(); 104 for (Node child : this.children) { 105 child.output(); 106 } 107 } 108 }
PS:本人比较喜欢设计模式,因此在写算法的时候也会想用一些设计模式里面的知识,其实在ACM比赛中,这样是不好的,效率低。
如果有哪里不清楚的,可以给我留言!
转载于:https://www.cnblogs.com/pinxiong/p/4001209.html
POJ 1057 File Mapping 最详细的解题报告相关推荐
- hihoCoder 1052 基因工程 最详细的解题报告
题目来源:基因工程 解题思路:假设基因序列长度为N,则需要计算基因序列前K个和后K个相同所需要的最少改变次数sum. 假设基因序列为 ATACGTCT (即M=8),K=6:interval=M-K= ...
- hihoCoder 1114 小Hi小Ho的惊天大作战:扫雷·一 最详细的解题报告
题目来源:小Hi小Ho的惊天大作战:扫雷·一 解题思路:因为只要确定了第一个是否有地雷就可以推算出后面是否有地雷(要么为0,要么为1,如果不是这两个值就说明这个方案行不通),如果两种可能中有一种成功, ...
- POJ 2800 垂直直方图 解题报告
POJ 2800 垂直直方图 解题报告 编号:2800 考查点:简单计算题 思路: 用gets()读入4行数据,然后按字符统计,显示的时候有点小处理即可. 提交情况: 感觉POJ的测试数据有点骗人 ...
- POJ 2745 显示器 解题报告
POJ 2745 显示器 解题报告 编号:2745 考查点:模拟 思路:抽象出来,计算器显示是7个笔画,然后建立数组表示各笔画被覆盖情况,不过这个是我看了书之后实现的,方法果真经典. 提交情况:比 ...
- 训练时发生的错误:Couldn‘t open shared file mapping: <000001910A228862>, error code: <1455>
今天在运行TTSR时第14个epoch时发生了以下错误: 看到一篇博客有写: RuntimeError: Couldn't open shared file mapping: <torch_37 ...
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何求 ...
- POJ 2054 Color a Tree解题报告
题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...
- POJ 1797 Heavy Transportation 解题报告
分类:图论,生成树,最短路,并查集 作者:ACShiryu 时间:2011-7-28 地址:ACShiryu's Blog Heavy Transportation Time Limit: 3000M ...
- 解题报告(四)生成函数(ACM/ OI)
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...
最新文章
- Codeforces Round #370 (Div. 2)E. Memory and Casinos[期望概率+线段树区间合并]详细推导
- 洛谷—— P1605 迷宫
- 韩顺平循序渐进学java 第19讲 多维数组
- 计算机学院去微软的大牛的面试经验
- python工具包_python 工具包
- boost::gil::view_is_basic用法的测试程序
- LevelDB源码解读
- mod_wsgi的工作模式和配置
- 低级程序员才喜欢写注释!
- matlab malloc,[原创]Matlab 高性能代码编写(三)内存是要先分配再使用的
- excel制作录入和查询系统_Excel进销存管理系统,完整函数应用,出入查询库存更新自动显示...
- 管理例程——数据库管理员
- 锁php_php+redis实现分布式锁
- jQuery Api 学习(一、jQuery 概念 和 jQuery 文档)
- Windows 搭建 翼龙面板 ( Pterodactyl ) 前端 教程
- GB28181国标错误码
- 服务器系统做双备份,云服务器如何做双机热备份
- php 二维数组根据某个键值倒叙、升序排序
- 悟已往之不谏,知来者之可追;实迷途其未远,觉今是而昨非
- 如何抓取安卓APP日志?