题目描述
咕咕东的雪梨电脑的操作系统在上个月受到宇宙射线的影响,时不时发生故障,他受不了了,想要写一个高效易用零bug的操作系统 —— 这工程量太大了,所以他定了一个小目标,从实现一个目录管理器开始。前些日子,东东的电脑终于因为过度收到宇宙射线的影响而宕机,无法写代码。他的好友TT正忙着在B站看猫片,另一位好友瑞神正忙着打守望先锋。现在只有你能帮助东东
初始时,咕咕东的硬盘是空的,命令行的当前目录为根目录 root。
目录管理器可以理解为要维护一棵有根树结构,每个目录的儿子必须保持字典序。
【样例输入】
1
22
MKDIR dira
CD dirb
CD dira
MKDIR a
MKDIR b
MKDIR c
CD …
MKDIR dirb
CD dirb
MKDIR x
CD …
MKDIR dirc
CD dirc
MKDIR y
CD …
SZ
LS
TREE
RM dira
TREE
UNDO
TREE
【样例输出】
OK
ERR
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
9
dira
dirb
dirc
root
dira
a
b
c
dirb
x
dirc
y
OK
root
dirb
x
dirc
y
OK
root
dira
a
b
c
dirb
x
dirc
y
问题分析
1、首先构造存放指令和存放目录信息的结构体Command和Directory
2、MKDIR: 新建目录:先检查当前目录下是否已经存在要新建的目录,若没有,则新建一个,并更新这个目录所属的目录的size。
3、RM: 删除目录:基本上和新建目录是相反的操作。
4、CD: 进入目录:若是"…",则要进入上一级目录,否则查找目录是否存在于当前目录下,并进入那个目录。
5、SZ: 输出当前目录大小
6、LS: 输出当前直接子目录:看直接子目录的孩子数量。
7、TREE: 输出子树:为了节约时间,对每个结点实现懒更新,对每个结点都有一个pre数组和bck数组用来存储前序遍历的前几个点,和后序的几个点。当节点不需要更新时,速度就会非常快。这里在每个目录使用一个vector容器保存TREE操作的输出,并使用一个标志update判断该目录是否被更改,若未更改,则遇到TREE操作时可直接输出上次的输出结果,以达到减少时间复杂度的目的。
所有的操作指令都放在一个vector数组里,并记录是哪个目录到哪个目录进行的操作,便于回溯。
8、UNDO: 撤回,查看存储指令的数组里末尾是什么指令,针对那个指令,进行相应的撤回。使用vector容器保存可撤销的命令。
#include<iostream>
#include<map>
#include<string>
#include<vector>
using namespace std;
const string c[7] = {"MKDIR","RM","CD","SZ","LS","TREE","UNDO" };
string temp;
struct Direct
{
public:
string name;
map<string, Direct*>children;
Direct* parent;
int tsize;
bool update;
vector<string> ten;
Direct(string na, Direct* p)
{
this->name = na;
this->parent = p;
this->tsize = 1;
}
bool addchild(Direct* ch)
{
if (children.find(ch->name) != children.end())
{
return false;
}
else
{
children[ch->name] = ch;
maintain(+ch->tsize);
return true;
}
}
Direct* getchild(string s)
{
auto it = children.find(s);
if (it == children.end())return NULL;
else return it->second;
}
void maintain(int d)
{
update = true;
tsize = tsize + d;
if (parent != NULL)
{
parent->maintain(d);
}
}
Direct* mkdir(string name)
{
if (children.find(name) != children.end())
return NULL;
else
{
Direct* ch = new Direct(name, this);
children[name] = ch;
maintain(+1);
return ch;
}
}
Direct* rm(string name)
{
auto it = children.find(name);
if (it == children.end())
return NULL;
else
{
Direct* iter = it->second;
maintain(-1 * it->second->tsize);
it = children.erase(it);
return iter;
}
}
Direct* cd(string name)
{
if (name == "..")
{
return this->parent;
}
else
{
return getchild(name);
}
}
void sz()
{
printf("%d\n", this->tsize);
}
void ls()
{
int sz = children.size();
if (sz == 0)printf("EMPTY\n");
else if (sz <= 10)for (auto& entry : children)printf("%s\n", entry.first.c_str());
else
{
auto it = children.begin();
for (int i = 0; i < 5; i++, it++)printf("%s\n", it->first.c_str());
printf("...\n");
it = children.end();
for (int i = 0; i < 5; i++)it--;
for (int i = 0; i < 5; i++, it++)printf("%s\n", it->first.c_str());
}
}
void tree()
{
if (tsize == 1)printf("EMPTY\n");
else if (tsize <= 10)
{
if (this->update)
{
ten.clear();
treeall(&ten);
this->update = false;
}
for (int i = 0; i <ten.size(); i++)printf("%s\n", ten.at(i).c_str());
}
else {
if (this->update)
{
ten.clear();
treefirst(5, &ten);
treelast(5, &ten);
this->update = false;
}
for (int i = 0; i < 5; i++)printf("%s\n", ten.at(i).c_str());
printf("...\n");
for (int i = 9; i >= 5; i--)printf("%s\n", ten.at(i).c_str());
}
}
private:
void treeall(vector<string>* bar)
{
bar->push_back(name);
for (auto& entry : children)
entry.second->treeall(bar);
}
void treefirst(int num, vector<string>* bar)
{
bar->push_back(name);
if (--num == 0)return;
int n = children.size();
auto it = children.begin();
while (n--)
{
int sts = it->second->tsize;
if (sts >= num) {
it->second->treefirst(num, bar);
return;
}
else {
it->second->treefirst(sts, bar);
num -= sts;
}
it++;
}
}
void treelast(int num, vector<string>* bar)
{
int n = children.size();
auto it = children.end();
while (n--)
{
it--;
int sts = it->second->tsize;
if (sts >= num) {
it->second->treelast(num, bar);
return;
}
else {
it->second->treelast(sts, bar);
num -= sts;
}
}
bar->push_back(name);
}
};
struct command
{
int type;
Direct* tempdir;
string arg;
command(string s) {
for (int i = 0; i < 7; i++)
{
if (c[i] == s)
{
type = i; break;
}
}
if (type < 3)
cin >> temp, arg = temp;
}
};
void solve() {
int n;
cin >> n;
Direct* now = new Direct("root", NULL);
vector<command*>cmdlist;
for (int i = 0; i < n; i++)
{
cin >> temp;
command* cmd = new command(temp);
int val = cmd->type; Direct* t;
bool su = false;
switch (val)
{
case 0://MKDIR
t=now->mkdir(cmd->arg);
if (t == NULL)
printf("ERR\n");
else {
cmd->tempdir =t;
printf("OK\n");
cmdlist.push_back(cmd);
}
break;
case 1:
t=now->rm(cmd->arg);
if (t == NULL)
printf("ERR\n");
else {
printf("OK\n");
cmd->tempdir = t;
cmdlist.push_back(cmd);
}
break;
case 2://CD.
t = now->cd(cmd->arg);
if (t == NULL)
printf("ERR\n");
else {
printf("OK\n");
cmd->tempdir = now;
now = t;
cmdlist.push_back(cmd);
}break;
case 3://SZ
now->sz(); break;
case 4://LS
now->ls(); break;
case 5://TREE
now->tree(); break;
case 6://UNDO
{
while (!su && cmdlist.size())
{
cmd = cmdlist.back(); cmdlist.pop_back();
if (cmd->type == 0)
{
su = now->rm(cmd->arg) != NULL; break;
}
else if (cmd->type == 1)
{
su = now->addchild(cmd->tempdir); break;
}
else if (cmd->type == 2)
{
now = cmd->tempdir; su = true; break;
}
}
printf(su ? "OK\n" : "ERR\n");
break;
}
}
}
}
int main() {
int n;
cin >> n;
while (n--)
solve();
}