POJ 1057 File Mapping The most detailed problem solving report

Source: Internet
Author: User

Title Source: POJ 1057 File Mapping

The main topic: like My Computer display folder and file information, in the same level of directory, folders in the front of the file and folder order, the same level directory of files alphabetically. The file begins with ' F ', the folder begins with ' d ', ' * ' indicates the end of a case, ' # ' means that all input ends.

Problem-solving ideas: Recursive creation, the final recursive output. The algorithm is relatively simple, I will not elaborate, specific look at the code:

Specific algorithm (Java version, can be run directly)

1 ImportJava.util.*;2 3  Public classMain {4      Public Static voidMain (string[] args) {5Scanner input =NewScanner (system.in);6Node root =NULL;7Node parent =NULL;8String line = "";9         intCount = 1;Ten          Do { OneRoot =NewDirectorynode ("ROOT",NULL); AParent =Root; -              Do { -line =Input.next (); the                 if(Line! =NULL&&!Line.isempty ()) { -line =Line.trim (); -Node childnode=NULL; -                     if(Line.startswith ("F")) { +Childnode=NewFilenode (line,parent); - Parent.children.add (childnode); +}Else if(Line.startswith ("D")) { AChildnode=NewDirectorynode (line,parent); at Parent.children.add (childnode); -Parent=Childnode; -}Else if(Line.startswith ("]")) { -                         if(parent!=NULL){ -Parent=parent.parent; -                         } in                     } -                 } to} while(!line.startswith ("*")); +System.out.println (String.Format ("DATA SET%s:", count++)); - root.output (); the             if(!line.startswith ("#")){ * System.out.println (); $             }Panax Notoginseng} while(!line.startswith ("#")); -     } the } +  A Abstract classNode { the      PublicString name; +      PublicNode parent; -      PublicString prefix; $      PublicList<node>children; $  -      Publicnode (String name, node parent) { -          This. Name =name; the          This. Parent =parent; -          This. prefix = "";Wuyi         if( This. Parent! =NULL) { the              This. prefix = This. Parent.prefix + This. prefix; -         } Wu          This. Children =NewArraylist<node>(); -     } About  $      Public voidsort () { -Collections.sort ( This. Children,NewComparator<node>() { -              Public intCompare (node left, node right) { -                 if(leftinstanceofDirectorynode) { A                     return-1; +}Else{ the                     if(RightinstanceofFilenode) { -                         returnLeft.name.compareTo (right.name); $}Else{ the                         return1; the                     } the                 } the             } -         }); in     } the  the      Public Abstract voidoutput (); About } the  the classFilenodeextendsNode { the  +      PublicFilenode (String name, Node parent) { -         Super(name, parent); the     }Bayi  the @Override the      Public voidoutput () { -         if( This. Parent! =NULL) { -              This. prefix = This. Parent.prefix + This. prefix; the         } theSystem.out.println ( This. prefix + This. Name); the     } the } -  the classDirectorynodeextendsNode { the  the      PublicDirectorynode (String name, Node parent) {94         Super(name, parent); the     } the  the @Override98      Public voidoutput () { About         if( This. Parent! =NULL) { -              This. prefix = This. Parent.prefix + "| ";101         }102System.out.println ( This. prefix + This. Name);103          This. Sort ();104          for(Node Child: This. Children) { the child.output ();106         }107     }108}

PS: I prefer to design patterns, so in the writing algorithm will also want to use some design patterns inside the knowledge, in fact, in the ACM competition, this is not good, low efficiency.

If there is not clear, can give me a message!

POJ 1057 File Mapping The most detailed problem solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.