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();
}
}
[Java] 202012-3 带配额的文件系统 (60分 debugging)
最新推荐文章于 2022-06-04 19:30:08 发布