1.题目
202012-3 带配额的文件系统
2.数据结构与算法
DS:tree
A:大模拟,按图索骥
3.源代码
#include <string>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;
struct node{
string name="root";//名称
vector<struct node*> child;//孩子节点指针
struct node* parent=NULL;//父节点指针
ll ld=0,lr=0,sizec=0,sizeac=0,size=0;
// ld:孩子文件配额,lr:后代文件配额,sizec:孩子文件大小,sizeac:后代文件大小,size:普通文件大小
bool type=false;//文件类型,false:目录文件;true:普通文件。
};
node root;//根节点
//判断p结点及所有祖先结点,后代文件配额是否满足
bool panduan(node *p,ll bianhua){
if(p==NULL) return true;
if((p->lr==0 || p->sizeac+bianhua<=p->lr) && panduan(p->parent,bianhua)){
//当前结点后代文件配额满足,且所有祖先结点后代文件配额均满足
p->sizeac+=bianhua;return true;
}
else return false;
}
vector<string> v;//保存路径
//C操作
// C /A/B/1 1024
void opec(string s,ll size){
string dir="";
//处理字符串,不必考虑v为空的情况
v.clear();//清空v
//获取路径
for(ll i=1;i<s.size();i++){
if(s[i]=='/'){
v.push_back(dir);dir="";continue;}
dir+=s[i];
}
v.push_back(dir);
node *p=&root;//当前结点指针
ll len;//长度
bool isp;//标记
bool flag=true;//标记目录是否是第一个被创建的
node *huisu;
ll ii;
//处理前面目录
for(ll i=0;i<v.size()-1;i++){
len=p->child.size();
isp=true;
for(ll j=0;j<len;j++){
//找到该目录
if(p->child[j]->name==v[i]){
//该目录为普通文件,此次操作失败
if(p->child[j]->type){
cout<<"N\n";return;}
//该目录已存在,进行下层目录查找
else{
p=p->child[j];//修改当前查找结点的指针