题目来源: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 最详细的解题报告的更多相关文章

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

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

  2. POJ 1046 Color Me Less 最详细的解题报告

    题目来源:POJ 1046 Color Me Less 题目大意:每一个颜色由R.G.B三部分组成,D=Math.sqrt(Math.pow((left.red - right.red), 2)+ M ...

  3. POJ 1063 Flip and Shift 最详细的解题报告

    题目来源:Flip and Shift 题目大意:一个椭圆形的环形容器中有黑色和白色两种盘子,问你是否可以将黑色的盘子连续的放在一起.你可以有以下两种操作: 1.顺时针旋转所有的盘子 2.顺时针旋转3 ...

  4. POJ 1047 Round and Round We Go 最详细的解题报告

    题目链接:Round and Round We Go 解题思路:用程序实现一个乘法功能,将给定的字符串依次做旋转,然后进行比较.由于题目比较简单,所以不做过多的详解. 具体算法(java版,可以直接A ...

  5. POJ 1050 To the Max 最详细的解题报告

    题目来源:To the Max 题目大意:给定一个N*N的矩阵,求该矩阵中的某一个矩形,该矩形内各元素之和最大,即最大子矩阵问题. 解题方法:最大子序列之和的扩展 解题步骤: 1.定义一个N*N的矩阵 ...

  6. POJ 1095 Trees Made to Order 最详细的解题报告

    题目来源:Trees Made to Order 题目大意:根据下面的规则给一棵二叉树编号: 规则1:如果二叉树为空,则编号为0: 规则2:如果二叉树只有一个节点,则编号为1: 规则3:所有含有m个节 ...

  7. poj 2739 Sum of Consecutive Prime Numbers 解题报告

    题目链接:http://poj.org/problem?id=2739 预处理出所有10001以内的素数,按照递增顺序存入数组prime[1...total].然后依次处理每个测试数据.采用双重循环计 ...

  8. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  9. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

随机推荐

  1. Unit4-窝窝之昆崚

    全文共3012字,推荐阅读时间10~15分钟. 文章共分五个部分: 作业分析 烘烤OO的精华 评测相关 课程体验感受 一些小小的建议 作业分析 Unit4要求我们实现UML解析器,迭代过程主要是增加对 ...

  2. java.net.URISyntaxException 问题解决

    先上代码: Properties pro = PropertyUtil.getPropertiesInfo("xxx.properties"); String url = pro. ...

  3. Java—— 一点关于String的转换

    在Java学习中,恐怕我们遇到的最多的就是有关String与其他类型的转换了,我们来看一张图: 我们看到对于8种基本数据类型,除去byte和short类型没有外,其他的都有,值得注意的是可以把char ...

  4. 其他函数-web_concurrent

    web_concurrent_start函数是并发组开始的标记.组中所有的函数是并发执行的,并发组的结束符为web_concurrent_end 函数. 在并发组中,可以包含的函数有: web_url ...

  5. 硬件对同步的支持-TAS和CAS指令

    目录 Test and Set Compare and Swap 使用CAS实现线程安全的数据结构. 现在主流的多处理器架构都在硬件水平上提供了对并发同步的支持. 今天我们讨论两个很重要的硬件同步指令 ...

  6. WARN deploy.SparkSubmit$$anon$2: Failed to load org.apache.spark.examples.sql.streaming.StructuredNetworkWordCount.

    前言 今天运行Spark Structured Streaming官网的如下 ./bin/run-example org.apache.spark.examples.sql.streaming.Str ...

  7. java List的初始化

    今天在处理生成excel的时候用到了java的list,但是需要直接赋值固定的几个变量,如果先初始化然后add的方法: List<String> name = new ArrayList( ...

  8. Nginx 从入门到放弃(五)

    nginx的rewrite重写 nginx具有将一个路由经过加工变形成另外一个路由的功能,这就叫做重写. 重写中用到的指令 if (条件) {} 设定条件,再进行重写 set # 设定变量 retur ...

  9. I/O模式及select、 poll、 epoll

    I/O多路复用技术 复用技术(multiplexing)并不是新技术而是一种设计思想,在通信和硬件设计中存在频分复用.时分复用.波分复用.码分复用等.在日常生活中复用的场景也非常多.从本质上来说,复用 ...

  10. Sharepoint 2013设置customErrors

    原文地址:http://www.cnblogs.com/renzh/archive/2013/03/05/2944309.html#3407239 一.首先设置IIS中的Web.config文件 找到 ...