POJ 1057 File Mapping 最詳細的解題報告

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   java   for   檔案   

題目來源:POJ 1057 File Mapping

題目大意:像我的電腦那樣顯示檔案夾和檔案資訊,其中在同一級目錄內,檔案夾排在檔案的前面並且檔案夾的順序不變,同一級目錄中檔案按字母序排列。檔案以‘f’開頭,檔案夾以‘d’開頭,‘*’表示一個case的結束,‘#’表示所有輸入內容結束。

 

解題思路:遞迴建立,最後遞迴輸出。演算法比較簡單,我就不細說了,具體看代碼:

 

具體演算法(java版,可以直接運行)

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

 

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

 

POJ 1057 File Mapping 最詳細的解題報告

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.