[Java] 202012-3 带配额的文件系统 (60分 debugging)

import java.io.*;
import java.util.*;

class FileSystem
{
    private class Node 
    {
        // File
        private long filesize;

        // Directory
        private long LD;
        private long LR;
        private long LD_size;
        private long LR_size;
        private Map<String, Node> map;

        // Common
        private Node father;
        private boolean isDir;

        public Node(Node father, boolean isDir, long LD, long LR, Map<String, Node> map)
        {
            this.father = father;
            this.isDir = isDir;
            this.LD = LD;
            this.LR = LR;
            this.map = map;
        }
        public boolean contains(String child)
        {
            if (map.containsKey(child))
            {
                return true;
            }
            return false;
        }
        public Node get(String child)
        {
            return map.get(child);
        }
        public void put(String child, Node node)
        {
            map.put(child, node);
            return;
        }
        public void remove(String child)
        {
            map.remove(child);
            return;
        }
        public void setL(long LD, long LR)
        {
            this.LD = LD;
            this.LR = LR;
        }
    }

    private Node root;
    public FileSystem()
    {
        this.root = new Node(null, true, 0, 0, new HashMap<>());
    }
    
    public boolean create(String file_path, long file_size)
    {
        Node r = this.root;
        String[] path = file_path.split("/");
        for (int i = 0; i < path.length; i++)
        {
            String child = path[i];
            // file
            if (i == path.length - 1) 
            {
                if (r.isDir && r.contains(child))
                {
                    r = r.get(child);
                    if (r.isDir)
                    {
                        return false;
                    }
                    else
                    {
                        remove(file_path);
                        create(file_path, file_size);
                        return true;
                    }
                }
                else if (r.isDir)
                {
                    Node newNode = new Node(r, false, 0, 0, null);
                    newNode.filesize = file_size;
                    if (r.LD != 0 && r.LD < r.LD_size + file_size)
                    {
                        return false;
                    }
                    for (Node temp_r = r; temp_r != null; temp_r = temp_r.father)
                    {
                        if (temp_r.LR != 0 && temp_r.LR < temp_r.LR_size + file_size) {
                            return false;
                        }
                    }
                    r.LD_size += file_size;
                    for (Node temp_r = r; temp_r != null; temp_r = temp_r.father)
                    {
                        temp_r.LR_size += file_size;
                    }
                    r.put(child, newNode);
                }
            }
            // direcoty
            else 
            {
                if (r.isDir && r.contains(child)) 
                {
                    r = r.get(child);
                    if (!r.isDir)
                    {
                        return false;
                    }
                }
                else
                {
                    Node newNode = new Node(r, true, 0, 0, new HashMap<>());
                    r.put(child, newNode);
                    r = newNode;
                }
            }
        }
        return true;
    }

    public boolean remove(String file_path)
    {
        Node r = this.root;
        String[] path = file_path.split("/");
        for (int i = 0; i < path.length; i++)
        {
            String child = path[i];
            // the file to remove
            if (i == path.length - 1)
            {
                if (r.isDir && r.contains(child))
                {
                    Node node = r.get(child);
                    long size = 0;
                    if (node.isDir)
                    {
                        size = node.LR_size;
                    }
                    else
                    {
                        size = node.filesize;
                        r.LD_size -= size;
                    }
                    for (Node temp_r = r; temp_r != null; temp_r = temp_r.father)
                    {
                        temp_r.LR_size -= size;
                    }
                    r.remove(child);
                }
            }
            else
            {
                if (r.isDir && r.contains(child))
                {
                    r = r.get(child);
                }
                else
                {
                    break;
                }
            }
        }
        return true;
    }

    public boolean quota(String file_path, long LD, long LR)
    {
        Node r = this.root;
        String[] path = file_path.split("/");
        if (path.length == 0)
        {
            if (LD != 0 && LD < r.LD_size) {
                return false;
            }
            if (LR != 0 && LR < r.LR_size) {
                return false;
            }
        }
        else
        {
            for (int i = 0; i < path.length; i++)
            {
                String child = path[i];
                if (r.contains(child))
                {
                    r = r.get(child);
                    if (!r.isDir)
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            if (LD != 0 && r.LD_size > LD || LR != 0 && r.LR_size > LR)
            {
                return false;
            }
            r.setL(LD, LR);
        }
        return true;
    }

}

public class Main
{
    public Main() throws IOException
    {
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

        FileSystem fs = new FileSystem();
        int n = Integer.parseInt(f.readLine());
        StringTokenizer st = null;
        while ((n--) > 0)
        {
            st = new StringTokenizer(f.readLine());
            boolean succeed = false;
            String op = st.nextToken();
            String file_path = st.nextToken();
            switch(op)
            {
                case "C":
                    long file_size = Integer.parseInt(st.nextToken());
                    succeed = fs.create(file_path, file_size);
                    break;
                case "R":
                    succeed = fs.remove(file_path);
                    break;
                case "Q":
                    long LD = Long.parseLong(st.nextToken());
                    long LR = Long.parseLong(st.nextToken());
                    succeed = fs.quota(file_path, LD, LR);
                    break;
            }
            // output
            if (succeed)
            {
                out.println('Y');
            }
            else
            {
                out.println('N');
            }
        }
        out.close();
        f.close();
    }

    public static void main(String[] args) throws IOException 
    {
        new Main();
    }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值