日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 1057 File Mapping 最详细的解题报告

發(fā)布時間:2025/4/9 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 1057 File Mapping 最详细的解题报告 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目來源:POJ 1057 File Mapping

題目大意:像我的電腦那樣顯示文件夾和文件信息,其中在同一級目錄內(nèi),文件夾排在文件的前面并且文件夾的順序不變,同一級目錄中文件按字母序排列。文件以‘f’開頭,文件夾以‘d’開頭,‘*’表示一個case的結(jié)束,‘#’表示所有輸入內(nèi)容結(jié)束。

?

解題思路:遞歸創(chuàng)建,最后遞歸輸出。算法比較簡單,我就不細(xì)說了,具體看代碼:

?

具體算法(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:本人比較喜歡設(shè)計模式,因此在寫算法的時候也會想用一些設(shè)計模式里面的知識,其實在ACM比賽中,這樣是不好的,效率低。

?

如果有哪里不清楚的,可以給我留言!

?

轉(zhuǎn)載于:https://www.cnblogs.com/pinxiong/p/4001209.html

總結(jié)

以上是生活随笔為你收集整理的POJ 1057 File Mapping 最详细的解题报告的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。