题目来源: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. @atcoder - AGC034F@ RNG and XOR

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个值域在 [0, 2^N) 的随机数生成器,给定参数 A[ ...

  2. conda 切换为国内源

    添加清华源 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda con ...

  3. Android学习笔记上下文菜单

    布局文件main_activity.xml <?xml version="1.0" encoding="utf-8"?> <RelativeL ...

  4. 如何 SSH 到 Linux 服务器里的特定目录及执行命令?

    你是不是有遇到过这样的场景?使用 SSH 命令进入到服务器,然后再用 cd 命令进入到对应目录,再继续进行你的工作. 这种操作对于新手来讲特别常见,良许之前也是这样.在本文,老司机将带你来进行更高效的 ...

  5. vulstack红队评估(三)

    一.环境搭建: ①根据作者公开的靶机信息整理 没有虚拟机密码,纯黑盒测试...一共是5台机器,目标是拿下域控获取flag文件   ②虚拟机网卡设置 centos双网卡模拟内外网: 外网:192.168 ...

  6. 超详细Maven技术应用指南

    该文章,GitHub已收录,欢迎老板们前来Star! GitHub地址: https://github.com/Ziphtracks/JavaLearningmanual 搜索关注微信公众号" ...

  7. uni-app之实现分页

    一.下载库 官方文档地址为:https://ext.dcloud.net.cn/plugin?id=32 点击下载zip压缩包即可,下载完毕后解压到放置前端相关组件目录,即components目录. ...

  8. @EnableHystrixDashboard注解无法使用情况

    本人使用的是springboot 2.1.3,在引入hystrix仪表盘时,发现@EnableHystrixDashboard注解无法使用. 原来的pom配置 度娘之后,发现是springboot版本 ...

  9. DDoS压力测试工具t50

    site: https://sourceforge.net/projects/t50/ 例子:t50 192.168.1.1 --flood--protocol T50|TCP|UDP|ICMP--t ...

  10. 四. django template模版

    往前端浏览器pull一些字符串,这些字符串是一些数据, 那如果想让这些数据按我们的某种格式美化一点/增加样式/图片,就需要用到django提供的模版--模版就是为了让数据看起更美观. 加载模版 dja ...