题意:
咕咕东的雪梨电脑的操作系统在上个月受到宇宙射线的影响,时不时发生故障,他受不了了,想要写一个高效易用零bug的操作系统 —— 这工程量太大了,所以他定了一个小目标,从实现一个目录管理器开始。前些日子,东东的电脑终于因为过度收到宇宙射线的影响而宕机,无法写代码。他的好友TT正忙着在B站看猫片,另一位好友瑞神正忙着打守望先锋。现在只有你能帮助东东!
初始时,咕咕东的硬盘是空的,命令行的当前目录为根目录 root。
目录管理器可以理解为要维护一棵有根树结构,每个目录的儿子必须保持字典序。
现在咕咕东可以在命令行下执行以下表格中描述的命令:
命令
类型
实现
说明
MKDIR s
操作
在当前目录下创建一个子目录 s,s 是一个字符串
创建成功输出 “OK”;若当前目录下已有该子目录则输出 “ERR”
RM s
操作
在当前目录下删除子目录 s,s 是一个字符串
删除成功输出 “OK”;若当前目录下该子目录不存在则输出 “ERR”
CD s
操作
进入一个子目录 s,s 是一个字符串(执行后,当前目录可能会改变)
进入成功输出 “OK”;若当前目录下该子目录不存在则输出 “ERR”
特殊地,若 s 等于 “…” 则表示返回上级目录,同理,返回成功输出 “OK”,返回失败(当前目录已是根目录没有上级目录)则输出 “ERR”
SZ
询问
输出当前目录的大小
也即输出 1+当前目录的子目录数
LS
询问
输出多行表示当前目录的 “直接子目录” 名
若没有子目录,则输出 “EMPTY”;若子目录数属于 [1,10] 则全部输出;若子目录数大于 10,则输出前 5 个,再输出一行 “…”,输出后 5 个。
TREE
询问
输出多行表示以当前目录为根的子树的前序遍历结果
若没有后代目录,则输出 “EMPTY”;若后代目录数+1(当前目录)属于 [1,10] 则全部输出;若后代目录数+1(当前目录)大于 10,则输出前 5 个,再输出一行 “…”,输出后 5 个。若目录结构如上图,当前目录为 “root” 执行结果如下:
UNDO
特殊
撤销操作
撤销最近一个 “成功执行” 的操作(即MKDIR或RM或CD)的影响,撤销成功输出 “OK” 失败或者没有操作用于撤销则输出 “ERR”
input:
输入文件包含多组测试数据,第一行输入一个整数表示测试数据的组数 T (T <= 20);
每组测试数据的第一行输入一个整数表示该组测试数据的命令总数 Q (Q <= 1e5);
每组测试数据的 2 ~ Q+1 行为具体的操作 (MKDIR、RM 操作总数不超过 5000);
面对数据范围你要思考的是他们代表的 “命令” 执行的最大可接受复杂度,只有这样你才能知道你需要设计的是怎样复杂度的系统。
output:
每组测试数据的输出结果间需要输出一行空行。注意大小写敏感。
时空限制:
Time limit 6000 ms
Memory limit 1048576 kB
sample input:
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
sample output:
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
思路:
定义结构体,并在结构体中定义编写所需要用到的函数。定义两个结构体,其中一个结构体用来存放指令,一个结构体用来存放目录。新建、删除目录时需要先判断该操作的目录是否存在再进行相应的操作,目录的size值等也随之更新。其他的操作都在结构体中根据具体的需求进行编程,在建立的树结构中进行相应的操作。
代码:
#include<iostream>
#include<map>
#include<vector>
#include<string.h>
#include<algorithm>
#include<cstdio>
using namespace std;
char temp[20];
struct directory{
string name;
map<string,directory*> children;
directory* parent;
int streesize;
vector<string> *thedes;
bool updat;
directory(string cname,directory* cparent)
{
this->name=cname;
this->parent=cparent;
this->streesize=1;
this->updat=false;
children.clear();
thedes=new vector<string>;
}
public:
directory* getchild(const string &cname)
{
auto it=children.find(cname);
if(it==children.end()) return nullptr;
return it->second;
}
directory* mkdir(const string &cname)
{
if(children.find(cname)!=children.end()) return nullptr;
directory* ch=new directory(cname,this);
children[cname]=ch;
maintain(+1);
return ch;
}
directory* rm(const string &cname)
{
auto it=children.find(cname);
if(it==children.end()) return nullptr;
maintain(-1*(it->second->streesize));
children.erase(it);
return it->second;
}
directory *cd(const string &cname)
{
if(cname=="..") return this->parent;
return getchild(cname);
}
bool addchild(directory *ch)
{
if(children.find(ch->name)!=children.end()) return false;
children[ch->name]=ch;
maintain(+ch->streesize);
return true;
}
void maintain(int delta)
{
updat=true;
streesize=streesize+delta;
if(parent!=nullptr) parent->maintain(delta);
}
void sz()
{
printf("%d\n",this->streesize);
}
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(streesize==1) printf("EMPTY\n");
else if(streesize<=10)
{
if(this->updat)
{
thedes->clear();
//thedes.clear();
treeall(thedes);
this->updat=false;
}
for(int i=0;i<streesize;i++) printf("%s\n",thedes->at(i).c_str());
}
else
{
if(this->updat)
{
thedes->clear();
//thedes.clear();
treefirst(5,thedes);
treelast(5,thedes);
this->updat=false;
}
for(int i=0;i<5;i++) printf("%s\n",thedes->at(i).c_str());
printf("...\n");
for(int i=9;i>=5;i--) printf("%s\n",thedes->at(i).c_str());
}
}
private:
void treeall(vector<string> *ba)
{
ba->push_back(name);
//ba.push_back(name);
for(auto &entry: children) entry.second->treeall(ba);
}
void treefirst(int num,vector<string> *ba)
{
ba->push_back(name);
//ba.push_back(name);
if(--num==0) return;
int n=children.size();
auto it=children.begin();
while(n--)
{
int st=it->second->streesize;
if(st>=num){
it->second->treefirst(num,ba);
return;
}
else
{
it->second->treefirst(st,ba);
num=num-st;
}
it++;
}
}
void treelast(int num,vector<string> *ba)
{
int n=children.size();
auto it=children.end();
while(n--)
{
it--;
int st=it->second->streesize;
if(st>=num)
{
it->second->treelast(num,ba);
return;
}
else
{
it->second->treelast(st,ba);
num=num-st;
}
}
ba->push_back(name);
//ba.push_back(name);
}
};
struct command1{
const string typename1[7]={"MKDIR","RM","CD","SZ","LS","TREE","UNDO"};
int type;
string sp;
directory *tmp;
command1(const string &s)
{
tmp=nullptr;
for(int i=0;i<7;i++)
{
if(typename1[i]==s)
{
type=i;
if(i<3)
{
scanf("%s",temp);
sp=temp;
}
return;
}
}
}
};
void solve()
{
int n;
scanf("%d",&n);
directory* now=new directory("root",nullptr);
vector<command1*> clist;
clist.clear();
while(n--)
{
scanf("%s",temp);
command1* cmd=new command1(temp);
switch(cmd->type)
{
case 0: case 1:{
cmd->tmp=(cmd->type==0 ? now->mkdir(cmd->sp) : now->rm(cmd->sp));
if(cmd->tmp==nullptr) printf("ERR\n");
else
{
printf("OK\n");
clist.push_back(cmd);
}
break;
}
case 2:{
directory *ch=now->cd(cmd->sp);
if(ch==nullptr) printf("ERR\n");
else
{
printf("OK\n");
cmd->tmp=now;
now=ch;
clist.push_back(cmd);
}
break;
}
case 3:{
now->sz();
break;
}
case 4:{
now->ls();
break;
}
case 5:{
now->tree();
break;
}
case 6:{
bool suc=false;
while(!suc&&!clist.empty()){
cmd=clist.back();
clist.pop_back();
switch(cmd->type)
{
case 0:{
suc=now->rm(cmd->tmp->name)!=nullptr;
break;
}
case 1:{
suc=now->addchild(cmd->tmp);
break;
}
case 2:{
now=cmd->tmp;
suc=true;
break;
}
}
}
printf(suc ? "OK\n" : "ERR\n");
break;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
solve();
}
return 0;
}