#include <iostream>
#include <map>
#include <vector>
using namespace std;
const long long LLMAX = 9223372036854775807;
enum FileType { //文件类型枚举变量,分为普通文件和目录文件
file, folder
};
struct FileNode { //树形文件结构的结点
string name; //文件名
FileType type; //文件类型
long long size, children_size, LD, LR; //对于普通文件,size为文件大小,children_size、LD、LR均无意义;对于目录文件,size为后代文件总大小,children_size为孩子文件总大小,LD为目录配额,LR为后代配额
map<string, FileNode*> children; //存储所有孩子文件指针的map
FileNode* parent; //指向双亲目录的指针
FileNode (string n, FileType t, long long s = 0, FileNode* p = nullptr) {
name = n;
type = t;
size = s;
children_size = 0;
LD = LLMAX;
LR = LLMAX;
parent = p;
}
};
vector<string> SplitPath(string p) { //将文件路径分割成字符串数组,方便处理
p.push_back('/');
vector<string> ans;
if (p == "//") return ans;
int slash_idx = 0;
for (size_t i = 1; i < p.size(); i++) {
if (p[i] == '/') {
ans.push_back(p.substr(slash_idx + 1, i - slash_idx - 1));
slash_idx = i;
}
}
return ans;
}
int main()
{
FileNode root_folder("root_folder", folder);
int n;
cin >> n;
while (n--) {
string cmd, path;
cin >> cmd >> path;
vector<string> path_folders = SplitPath(path);
FileNode* p = &root_folder;
if (cmd == "C") { //如果命令为创建文件
long long fsize;
FileNode* new_f = nullptr;
bool file_exist = false;
cin >> fsize;
for (size_t i = 0; i < path_folders.size(); i++) { //判断要创建的文件是否已经存在
if (p->children.find(path_folders[i]) == p->children.end()) {
break; //找不到路径里的某些文件,说明要创建的文件不存在
} else if (i == path_folders.size() - 1 && p->children.find(path_folders[i])->second->type == folder || i < path_folders.size() - 1 && p->children.find(path_folders[i])->second->type == file) { //找到了路径里的某些文件,但是文件类型不符,指令失败
cout << "N" << endl;
goto done;
} else {
p = p->children.find(path_folders[i])->second;
}
if (i == path_folders.size() - 1) file_exist = true; //遍历完path_folders说明要创建的文件已经存在
}
if (file_exist) { //如果要创建的文件已经存在
if (p->parent->LD - p->parent->children_size < fsize - p->size) { //看看双亲目录的目录配额是否足够
cout << "N" << endl;
goto done;
}
FileNode* q = p->parent;
while (q != nullptr) { //再看看所有祖先文件的后代配额是否足够
if (q->LR - q->size < fsize - p->size) {
cout << "N" << endl;
goto done;
}
q = q->parent;
}
q = p->parent;
q->children_size += fsize - p->size; //把双亲目录的children_size更新一下
while (q != nullptr) { //把祖先文件的size更新一下
q->size += fsize - p->size;
q = q->parent;
}
p->size = fsize; //把要创建文件的size更新一下
} else { //如果要创建的文件不存在
if (root_folder.LR - root_folder.size < fsize) { //先看下根目录的后代配额是否足够
cout << "N" << endl;
goto done;
}
p = &root_folder;
for (size_t i = 0; i < path_folders.size() - 1; i++) { //遍历路径里的每个目录文件
if (p->children.find(path_folders[i]) == p->children.end()) { //如果目录文件不存在,就创建它
FileNode* q = new FileNode(path_folders[i], folder, 0, p);
p->children.insert({path_folders[i], q});
p = q;
} else if (p->children.find(path_folders[i])->second->LR - p->children.find(path_folders[i])->second->size < fsize) { //如果目录文件存在,但是后代配额不够了,则指令失败
cout << "N" << endl;
goto done;
} else {
p = p->children.find(path_folders[i])->second;
}
}
if (p->LD - p->children_size < fsize) { //判断要创建的文件的双亲目录的目录配额是否足够
cout << "N" << endl;
goto done;
}
FileNode* q = new FileNode(path_folders[path_folders.size() - 1], file, fsize, p); //创建新文件
p->children.insert({path_folders[path_folders.size() - 1], q});
FileNode* r = q->parent;
r->children_size += fsize; //把双亲目录的children_size更新一下
while (r != nullptr) { //把祖先文件的size更新一下
r->size += fsize;
r = r->parent;
}
}
cout << "Y" << endl;
} else if (cmd == "R") { //如果命令为删除文件
FileNode* q = nullptr;
for (size_t i = 0; i < path_folders.size(); i++) { //判断要删除的文件是否存在,如果不存在就不用处理了,直接认为指令成功
if (p->children.find(path_folders[i]) == p->children.end()) {
cout << "Y" << endl;
goto done;
} else {
p = p->children.find(path_folders[i])->second;
}
}
q = p->parent;
q->children.erase(p->name); //把双亲目录里指向要删除文件的指针给删除
if (p->type == file) q->children_size -= p->size; //如果要删除的文件是普通文件,则需要更新双亲目录的children_size
while (q != nullptr) { //把祖先文件的size更新一下
q->size -= p->size;
q = q->parent;
}
delete p; //本来应该递归删除p的p的所有后代结点的,但是懒得写了
cout << "Y" << endl;
} else if (cmd == "Q") { //如果命令为设置配额
long long new_LD, new_LR;
cin >> new_LD >> new_LR;
for (size_t i = 0; i < path_folders.size(); i++) { //判断要设置配额的文件是否存在,如果不存在则指令失败
if (p->children.find(path_folders[i]) == p->children.end()) {
cout << "N" << endl;
goto done;
}
else p = p->children.find(path_folders[i])->second;
}
if (p->type == file) { //如果要设置配额的文件是普通文件,则指令失败
cout << "N" << endl;
goto done;
}
if (new_LD == 0) new_LD = LLMAX; //如果目录配额的输入值为0,则认为目录配额为最大值
if (new_LR == 0) new_LR = LLMAX; //如果后代配额的输入值为0,则认为后代配额为最大值
if (p->children_size > new_LD || p->size > new_LR) { //如果更新配额后出现不满足的情况,则指令失败
cout << "N" << endl;
goto done;
} else { //更新配额
p->LD = new_LD;
p->LR = new_LR;
cout << "Y" << endl;
}
}
done: ;
}
return 0;
}
CCF CSP 202012-3 带配额的文件系统
最新推荐文章于 2024-03-08 10:35:43 发布