PKU3856 deltree 模拟

一次比赛里的题,模拟DOS里的dir、deltree命令,直接贴代码了,这种题目就感觉到java的String类强大了,话说spilt方法似乎有点小bug。。用正则表达式表示多个prtten都不可以,不解。。贴代码

  1import java.io.*;
  2import java.util.*;
  3class node
  4{
  5    HashMap<String,node> dir=new HashMap<String,node>();
  6    HashMap<String,Integer> file=new HashMap<String,Integer>();
  7    node per=null;
  8}
;
  9public class Main {
 10
 11    /**
 12     * @param args
 13     */

 14    static node head;
 15    static int total;
 16    static void del(node p)
 17    {
 18        Collection<node> dir=p.dir.values();
 19        Collection<Integer> tmp=p.file.values();
 20        for(Integer pos:tmp)
 21            total+=pos;
 22        for(node pos:dir)
 23            del(pos);
 24        p.dir.clear();
 25        p.file.clear();
 26        
 27    }

 28    public static void main(String[] args) throws IOException{
 29        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
 30        while(true)
 31        {
 32            head=new node();
 33            String str=in.readLine();
 34            if(str.equals(">exit")) break;
 35            node p=head;
 36            total=0;
 37            while(true)
 38            {
 39                if(str.length()==0break;
 40                str=str.substring(1);
 41                if(str.startsWith("cd"))
 42                {
 43                    str=str.substring(3);
 44                    if(str.charAt(0)=='\\')
 45                    {
 46                        while(p!=head) p=p.per;
 47                        str=str.substring(1);
 48                    }

 49                    else if(str.charAt(0)=='.')
 50                    {
 51                        p=p.per;
 52                        str=str.substring(2);
 53                    }

 54                    if(str.length()!=0)
 55                    {
 56                        while(str.indexOf('\\')!=-1)
 57                        {
 58                            String t=str.substring(0,str.indexOf('\\'));
 59                            str=str.substring(str.indexOf('\\')+1);
 60                            if(p.dir.containsKey(t))
 61                                p=p.dir.get(t);
 62                            else
 63                            {
 64                                node tmp=new node();
 65                                tmp.per=p;
 66                                p.dir.put(t, tmp);
 67                                p=tmp;
 68                            }

 69                        }

 70                        if(p.dir.containsKey(str))
 71                            p=p.dir.get(str);
 72                        else
 73                        {
 74                            node tmp=new node();
 75                            tmp.per=p;
 76                            p.dir.put(str, tmp);
 77                            p=tmp;
 78                        }

 79                    }

 80                    
 81                    str=in.readLine();
 82                }

 83                else if(str.startsWith("dir"))
 84                {
 85                    while(true)
 86                    {
 87                        str=in.readLine();
 88                        if(str.length()==0||str.charAt(0)=='>'break;
 89                        String sp[]=str.split(" ");
 90                        if(sp.length==2)
 91                        {
 92                            if(!p.file.containsKey(sp[0]))
 93                                p.file.put(sp[0], Integer.parseInt(sp[1]));
 94                        }

 95                        else
 96                        {
 97                            if(!p.dir.containsKey(sp[0]))
 98                            {
 99                                node tmp=new node();
100                                tmp.per=p;
101                                p.dir.put(sp[0], tmp);
102                            }

103                        }

104                    }

105                }

106                else
107                {
108                    str=str.substring(8);
109                    if(str.charAt(0)=='\\')
110                    {
111                        while(p!=head) p=p.per;
112                        str=str.substring(1);
113                    }

114                    while(str.indexOf('\\')!=-1)
115                    {
116                        String tmp=str.substring(0,str.indexOf('\\'));
117                        str=str.substring(str.indexOf('\\')+1);
118                        p=p.dir.get(tmp);
119                    }

120                    p=p.dir.get(str);
121                    del(p);
122                    str=in.readLine();
123                    
124                }

125            }

126            System.out.println(total);
127        }

128    }

129
130}

131
132

posted on 2010-10-12 23:15 yzhw 阅读(102) 评论(0)  编辑 收藏 引用 所属分类: data struct


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年10月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

公告

统计系统

留言簿(1)

随笔分类(227)

文章分类(2)

OJ

最新随笔

搜索

积分与排名

最新评论

阅读排行榜