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