POJ 1057 File Mapping 最详细的解题报告
题目大意:像我的电脑那样显示文件夹和文件信息,其中在同一级目录内,文件夹排在文件的前面并且文件夹的顺序不变,同一级目录中文件按字母序排列。文件以‘f’开头,文件夹以‘d’开头,‘*’表示一个case的结束,‘#’表示所有输入内容结束。
解题思路:递归创建,最后递归输出。算法比较简单,我就不细说了,具体看代码:
具体算法(java版,可以直接AC)
 import java.util.*;
 public class Main {
     public static void main(String[] args) {
         Scanner input = new Scanner(System.in);
         Node root = null;
         Node parent = null;
         String line = "";
         int count = 1;
         do {
             root = new DirectoryNode("ROOT", null);
             parent = root;
             do {
                 line = input.next();
                 if (line != null && !line.isEmpty()) {
                     line = line.trim();
                     Node childNode=null;
                     if (line.startsWith("f")) {
                         childNode=new FileNode(line,parent);
                         parent.children.add(childNode);
                     }else if (line.startsWith("d")) {
                         childNode=new DirectoryNode(line,parent);
                         parent.children.add(childNode);
                         parent=childNode;
                     }else if (line.startsWith("]")) {
                         if(parent!=null){
                             parent=parent.parent;
                         }
                     }
                 }
             } while (!line.startsWith("*"));
             System.out.println(String.format("DATA SET %s:", count++));
             root.output();
             if(!line.startsWith("#")){
                 System.out.println();
             }
         } while (!line.startsWith("#"));
     }
 }
 abstract class Node {
     public String name;
     public Node parent;
     public String prefix;
     public List<Node> children;
     public Node(String name, Node parent) {
         this.name = name;
         this.parent = parent;
         this.prefix = "";
         if (this.parent != null) {
             this.prefix = this.parent.prefix + this.prefix;
         }
         this.children = new ArrayList<Node>();
     }
     public void sort() {
         Collections.sort(this.children, new Comparator<Node>() {
             public int compare(Node left, Node right) {
                 if (left instanceof DirectoryNode) {
                     return -1;
                 }else{
                     if (right instanceof FileNode){
                         return left.name.compareTo(right.name);
                     }else{
                         return 1;
                     }
                 }
             }
         });
     }
     public abstract void output();
 }
 class FileNode extends Node {
     public FileNode(String name, Node parent) {
         super(name, parent);
     }
     @Override
     public void output() {
         if (this.parent != null) {
             this.prefix = this.parent.prefix + this.prefix;
         }
         System.out.println(this.prefix + this.name);
     }
 }
 class DirectoryNode extends Node {
     public DirectoryNode(String name, Node parent) {
         super(name, parent);
     }
     @Override
     public void output() {
         if (this.parent != null) {
             this.prefix = this.parent.prefix + "|     ";
         }
         System.out.println(this.prefix + this.name);
         this.sort();
         for (Node child : this.children) {
             child.output();
         }
     }
 }
PS:本人比较喜欢设计模式,因此在写算法的时候也会想用一些设计模式里面的知识,其实在ACM比赛中,这样是不好的,效率低。
如果有哪里不清楚的,可以给我留言!
POJ 1057 File Mapping 最详细的解题报告的更多相关文章
- OpenJudge 2775 文件结构“图”/ Poj 1057 FILE MAPPING
		1.链接地址: http://bailian.openjudge.cn/practice/2775 http://poj.org/problem?id=1057 2.题目: 总时间限制: 1000ms ... 
- POJ 1046 Color Me Less  最详细的解题报告
		题目来源:POJ 1046 Color Me Less 题目大意:每一个颜色由R.G.B三部分组成,D=Math.sqrt(Math.pow((left.red - right.red), 2)+ M ... 
- POJ 1063 Flip and Shift 最详细的解题报告
		题目来源:Flip and Shift 题目大意:一个椭圆形的环形容器中有黑色和白色两种盘子,问你是否可以将黑色的盘子连续的放在一起.你可以有以下两种操作: 1.顺时针旋转所有的盘子 2.顺时针旋转3 ... 
- POJ 1047 Round and Round We Go 最详细的解题报告
		题目链接:Round and Round We Go 解题思路:用程序实现一个乘法功能,将给定的字符串依次做旋转,然后进行比较.由于题目比较简单,所以不做过多的详解. 具体算法(java版,可以直接A ... 
- POJ 1050 To the Max  最详细的解题报告
		题目来源:To the Max 题目大意:给定一个N*N的矩阵,求该矩阵中的某一个矩形,该矩形内各元素之和最大,即最大子矩阵问题. 解题方法:最大子序列之和的扩展 解题步骤: 1.定义一个N*N的矩阵 ... 
- POJ 1095 Trees Made to Order 最详细的解题报告
		题目来源:Trees Made to Order 题目大意:根据下面的规则给一棵二叉树编号: 规则1:如果二叉树为空,则编号为0: 规则2:如果二叉树只有一个节点,则编号为1: 规则3:所有含有m个节 ... 
- poj 2739 Sum of Consecutive Prime Numbers 解题报告
		题目链接:http://poj.org/problem?id=2739 预处理出所有10001以内的素数,按照递增顺序存入数组prime[1...total].然后依次处理每个测试数据.采用双重循环计 ... 
- 广大暑假训练1(poj 2488) A Knight's Journey   解题报告
		题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A (A - Children of the Candy Corn) ht ... 
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
		题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ... 
随机推荐
- Android开发学习笔记DDMS的使用
			打开DDMS DDMS 的全称是Dalvik Debug Monitor Service,是 Android 开发环境中的Dalvik虚拟机调试监控服务. DDMS里面包含了:Device(设备) F ... 
- vulstack红队评估(一)
			一.环境搭建: 1.根据作者公开的靶机信息整理: 虚拟机初始所有统一密码:hongrisec@2019 因为登陆前要修改密码,改为了panda666... 2.虚拟网卡网络配置: ①Win7双 ... 
- springboot mybatis plus多数据源轻松搞定 (上)
			在开发中经常会遇到一个程序需要调用多个数据库的情况,总得来说分为下面的几种情况: 一个程序会调用不同结构的两个数据库. 读写分离,两个数据结构可能一样高,但是不同的操作针对不同的数据库. 混合情况,既 ... 
- 【初识Redis】
			1. 前言 1.1 Reis是什么 Redis 是 C 语言开发的一个开源的(遵从 BSD 协议)高性能键值对(key-value)的 NoSQL的内存数据库,可以用作缓存.消息中间件等:具有以下 ... 
- DOM-BOM-EVENT(6)
			6.BOM 6.1.什么是BOM? BOM(Browse Object Model),浏览器对象模型,没有相关标准,是约定俗成的东西,定义了一些操作浏览器的方法和属性,大部分方法都是通过window对 ... 
- Mac下搭建react开发环境
			安装node 官网下载 https://nodejs.org/en/ 双击安装,接下来都是默认选择即可,直至安装成功 测试是否安装成功,分别输入以下命令: node -v npm -v 如下图所示,说 ... 
- onunload对应的js代码为什么不能执行?和onbeforeunload的区别?
			为什么onunload对应的js代码不能执行? 为什么onbeforeunload才可以在离开页面时执行相应的js代码? 1.onunload和onbeforeunload都是在离开页面或者刷新页面的 ... 
- STL初步学习(map)
			3.map map作为一个映射,有两个参数,第一个参数作为关键值,第二个参数为对应的值,关键值是唯一的 在平时使用的数组中,也有点类似于映射的方法,例如a[10]=1,但其实我们的关键值和对应的值只能 ... 
- Python数据结构-树与树的遍历
			树:是一种抽象的数据类型 树的作用:用来模拟树状结构性质的数据集合 树的特点: 每个节点有零个或者多个节点 没有父节点的节点,叫做根节点 每一个根节点有且只有一个父节点 除了根节点外,每个节点可以分成 ... 
- Spring Aop基于注解的实现
			一.AspectOriented Programing,面向切面编程. AOP主要用于日志记录,性能统计,安全控制(权限控制),事务处理,异常处理等.将日志记录,性能统计,安全控制,事务处理,异常 ... 
