博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1057 File Mapping 最详细的解题报告
阅读量:4440 次
发布时间:2019-06-07

本文共 3784 字,大约阅读时间需要 12 分钟。

题目来源:

题目大意:像我的电脑那样显示文件夹和文件信息,其中在同一级目录内,文件夹排在文件的前面并且文件夹的顺序不变,同一级目录中文件按字母序排列。文件以‘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
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
(); 55 } 56 57 public void sort() { 58 Collections.sort(this.children, new Comparator
() { 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

你可能感兴趣的文章
在Asp.Net MVC中使用Repeater控件
查看>>
应用程序已被安全设置阻止
查看>>
找球号(一)
查看>>
开发小计(3)
查看>>
[Codevs] 1001 舒适的路线
查看>>
Deep Learning相关
查看>>
MySQL 树形结构 根据指定节点 获取其所有父节点序列
查看>>
hdu_5773_The All-purpose Zero(LIS)
查看>>
流程控制之while循环
查看>>
JSONObject和JSONArray区别及基本用法
查看>>
java多线程例子
查看>>
目标检测网络之 YOLOv3
查看>>
python 使用pyinstaller,pywin32打包.py成.exe应用程序
查看>>
AFNetworking封装思路简析
查看>>
C# 之 批量插入数据到 SQLServer 中
查看>>
Visual Studio使用中的问题
查看>>
salesforce零基础学习(七十九)简单排序浅谈 篇一
查看>>
zabbix的源码安装
查看>>
磁盘配额中quotacheck不能创建aquota.user和aquota.group文件的问题
查看>>
2014年生日
查看>>