[CSP]带配额的文件系统 python3满分题解

[CSP]带配额的文件系统 python3满分题解


前言

在这里插入图片描述

小白一枚,最近准备csp,记录一下题解过程。题目描述这里我就不重复了,大家可以自己看官网

一、思路

  1. 数据结构设计
    本题目中我使用字典树,①其中键用文件名标识:比如/A/B/C/3表示普通文件(后缀没有’/’),/A/B/C/表示目录文件(后面有反斜杠)。②对于目录文件,其对应的值为列表:[原来的目录配额,已经花费的目录配额,原来的后台配额,已经花费的后台配额,后代文件的名称列表],对于普通文件,其对应的值为[普通文件的大小]

二、代码

class Solution:
    def fileSystem(self,cmd):
#检查新建stack文件,增加内存为memory时候合法
        def checkCreate(stack,memory):
            n=len(stack)
            temp=file_system[stack[n-2]]
            if (temp[0]!=0 and temp[1]+memory>temp[0])or (temp[2]!=0 and temp[3]+memory>temp[2]):
                return False
            for i in range(n-3,-1,-1):
                if file_system[stack[i]][2]!=0 and file_system[stack[i]][3]+memory>file_system[stack[i]][2]:
                    return False
            return True
#增加stack路径文件,新增大小为memory
        def setMemory(stack,memory):      
            n=len(stack)
            temp=file_system[stack[n-2]]
            temp[1]+=memory
            temp[3]+=memory
            file_system[stack[n-2]]=temp
            for i in range(n-3,-1,-1):
                temp=file_system[stack[i]]
                temp[3]+=memory
                file_system[stack[i]]=temp
#删除文件后的内存更新操作
        def setMemoryDel(path,memory):
            stack=list()
            path_list=path.split('/')
            #如果删除的是目录
            if path[-1]=='/':
                stack.append('/')
                if len(path_list)>2:
                    for temp in path_list[1:len(path_list)-2]:
                        stack.append(stack[-1]+temp+'/')
                temp=file_system[stack[-1]]
                temp[3]-=memory
                temp[4].remove(path)
                        
            #如果删除的是普通的文件
            else:
                stack.append('/')
                if len(path_list)>1:
                    for temp in path_list[1:len(path_list)-1]:
                        stack.append(stack[-1]+temp+'/')
                temp=file_system[stack[-1]]
                temp[1]-=memory
                temp[3]-=memory
                temp[4].remove(path)
            file_system[stack[-1]]=temp
            for i in range(len(stack)-2,-1,-1):
                temp=file_system[stack[i]]
                temp[3]-=memory
                file_system[stack[i]]=temp  
        def dsf_delete(path):
            if path[-1]!='/':
                del file_system[path]
                return
            for file in file_system[path][4]:
                dsf_delete(file)
            del file_system[path]
#新建文件夹
        def create(path,memory):
            stack=list()
            min_flag=float('inf')
            path_list=path.split('/')
            stack.append('/')
            pre=stack[-1]
            for temp in path_list[1:len(path_list)-1]:
                stack.append(stack[-1]+temp+'/')
                if stack[-1][:-1] in file_system:
                    return 'N'
                if stack[-1] not in file_system:
                    file_system[pre][4].add(stack[-1])
                    file_system[stack[-1]]=[0,0,0,0,set()]
                    file_new.append(stack[-1])
                pre=stack[-1]
            stack.append(stack[-1]+path_list[-1])
            if stack[-1]+'/' in file_system:
                return 'N'
            else:
                if stack[-1] not in file_system:
                    if not checkCreate(stack.copy(),memory):                                
                        if file_new:
                            delete(file_new[0][:-1])
                        return 'N'
                    file_system[pre][4].add(stack[-1])
                    setMemory(stack,memory)
                else:
                    memory_pre=file_system[stack[-1]][0]
                    if not checkCreate(stack.copy(),memory-memory_pre):
                        return 'N'
                    setMemory(stack,memory-memory_pre)
                file_system[stack[-1]]=[memory]         
            return 'Y'
        def delete(path):
            if path in file_system.keys() and path!='/':
                memory_temp=file_system[path][0]
                del file_system[path]
                setMemoryDel(path,memory_temp)
                return 'Y'
            if path+'/' in file_system.keys():
                memory_temp=file_system[path+'/'][3]
                dsf_delete(path+'/')
                setMemoryDel(path+'/',memory_temp)
                return 'Y'
            #删除的是根目录
            if path=='/':
                for key in list(file_system.keys()):
                    if key!='/':
                        del file_system[key]
                temp=file_system['/']
                temp[1],temp[3]=0,0
                file_system['/']=temp
            return 'Y'                   
#更改配额操作
        def update(path,ld,lr):
            path=path+'/' if path!='/' else '/'
            ld,lr=int(ld),int(lr)
            if path not in file_system:
                return 'N'
            if ld!=0 and ld<file_system[path][1]:
                return 'N'
            if lr!=0 and lr <file_system[path][3]:
                return 'N'
            temp=file_system[path]
            temp[0]=ld
            temp[2]=lr
            file_system[path]=temp
            return 'Y'
        res=list()
        file_new=list()
        cmd_detail=cmd.split()
        global file_system
        if cmd_detail[0]=='C':
            res=create(''.join(cmd_detail[1]),float(cmd_detail[2]))
            return res
        
        elif cmd_detail[0]=='R':
            res=delete(''.join(cmd_detail[1]))
            return res
        else:
            res=update(''.join(cmd_detail[1]),float(cmd_detail[2]),float(cmd_detail[3]))
            return res
if __name__=='__main__':
    solution=Solution
    file_system=dict()
    file_system['/']=[0,0,0,0,set()]
    n=int(input())
    for i in range(n):
        print(solution.fileSystem(solution,input()))


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值