hdu 1413 文件系统 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1413
题目分析:中小型模拟。较水,细心就可以。
以文件夹(Dir)为节点建树,涉及到数据结构的课内知识(顺便复习上学期课的内容)其他类另一个文件(File),文件夹下可有子文件夹和文件,其余规则refer to题意。
code:
package hdu1413; import java.util.Scanner; class File{ String fileName; File next; public File(String n){ fileName=n; } } class Dir{ String dirName; Dir firstC,NextS,parent; File firstF; boolean isnull; public static void traversal(Dir root){ if(root==null)return; System.out.print("-"+root.dirName); traversal(root.NextS); System.out.print(" |"); traversal(root.firstC); } public boolean create(File f){ if(this.firstF==null){ this.firstF=f; this.isnull=false; return true; } else{ File p=this.firstF; if(p.fileName.equals(f.fileName))return false; while(p.next!=null){ p=p.next; if(p.fileName.equals(f.fileName))return false; } p.next=f; return true; } } public boolean delete(String fname){ if(this.firstF==null)return false; else if(this.firstF.fileName.equals(fname)){ //System.out.println("run here"); this.firstF=this.firstF.next; if(this.firstC==null&&this.firstF==null)this.isnull=true; return true; } else{ File p=this.firstF; while(p.next!=null){ if(p.next.fileName.equals(fname)){ p.next=p.next.next; return true; } p=p.next; } return false; } } public Dir enter(String dname){ if(this.firstC==null)return null; else{ Dir p=this.firstC; while(p!=null){ if(p.dirName.equals(dname))return p; p=p.NextS; } } return null; } public boolean remove(String dname){ if(this.firstC==null)return false; else if(this.firstC.dirName.equals(dname)){ //System.out.println("firstC.firstC exist?=="+this.firstC.firstC+"firstC.firstF exist?=="+this.firstC.firstF); //System.out.println(this.firstC.isnull); if(!this.firstC.isnull)return false; this.firstC=this.firstC.NextS; if(this.firstC==null&&this.firstF==null)this.isnull=true; return true; } else{ Dir p=this.firstC; while(p.NextS!=null){ if(p.NextS.dirName.equals(dname)&&p.NextS.isnull){ p.NextS=p.NextS.NextS; return true; } p=p.NextS; } } return false; }//数据结构课上老师教的remove都是返回被删除元素,我不大习惯…… public boolean establish(Dir d){ if(this.firstC==null){ this.firstC=d; this.isnull=false; } else{ Dir p=this.firstC; if(p.dirName.equals(d.dirName))return false; while(p.NextS!=null){ p=p.NextS; if(p.dirName.equals(d.dirName))return false; } p.NextS=d; } d.parent=this; return true; } public Dir(String n,Dir p){ dirName=n; parent=p; isnull=true; } public Dir(String n){ dirName=n; isnull=true; } } public class Main { static Scanner in; public static void main(String[] args) { Dir dirP,root=new Dir("\"); root.parent=root; dirP=root; String s,param; in=new Scanner(System.in); while(in.hasNext()){ s=in.nextLine(); switch(s.charAt(0)){ case'M':{ param=s.substring(3); if(param.equals("\")||param.equals("..")) System.out.println("directory already exist"); else if(dirP.establish(new Dir(param)))System.out.println("success"); else System.out.println("directory already exist"); break; } case'R':{ param=s.substring(3); if(dirP.remove(param))System.out.println("success"); else System.out.println("can not delete the directory"); break; } case'D':{ param=s.substring(7); if(dirP.delete(param))System.out.println("success"); else System.out.println("no such file"); break; } case'T':Dir.traversal(root);break; default: if(s.charAt(1)=='D'){ param=s.substring(3); if(param.equals(".."))dirP=dirP.parent; else if(param.equals("\"))dirP=root; else{ Dir p=dirP.enter(param); if(p==null){ System.out.println("no such directory"); break; } else dirP=p; } System.out.println("success"); } else{ param=s.substring(7); if(dirP.create(new File(param)))System.out.println("success"); else System.out.println("file already exist"); } } } } }/*1. CD ACM MD ACM CD ACM CREATE ACM MD ACM CD ACM CD RD ACM CD ACM RD ACM DELETE ACM CD .. RD ACM 2. CD ACM MD ACM CD ACM CREATE ACM MD ACM CREATE ACM MD ACM CD ACM CD RD ACM CD ACM RD ACM DELETE ACM CD .. RD ACM 3. MD ACM MD ACM CREATE ACM CREATE ACM RD ACM MD ACM 4. MD ACM MD ADM MD ADM CREATE ACM CREATE ADM CREATE ADM CD ADM CREATE ACM CD .. RD ADM */
PS:直接复制上面代码去交的下场就是…………wrong,为啥呢?由于packge,不建包就好了。
PSS:看来数据结构还是学得不够扎实……