题面
咕咕东的操作系统在上个月受到宇宙射线的影响,时不时发生故障,他受不了了,想要写一个高效易用零bug的操作系统 —— 这工程量太大了,所以他定了一个小目标,从实现一个目录管理器开始。
初始时,咕咕东的硬盘是空的,命令行的当前目录为根目录 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 个。 |
UNDO | 特殊 | 撤销操作 | 撤销最近一个 “成功执行” 的操作(即MKDIR或RM或CD)的影响,撤销成功输出 “OK” 失败或者没有操作用于撤销则输出 “ERR” |
输入文件包含多组测试数据,第一行输入一个整数表示测试数据的组数 T (T <= 20);
每组测试数据的第一行输入一个整数表示该组测试数据的命令总数 Q (Q <= 1e5);
每组测试数据的 2 ~ Q+1 行为具体的操作 (MKDIR、RM 操作总数不超过 5000)
每组测试数据的输出结果间需要输出一行空行。注意大小写敏感.
限制:
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
思路:
- 通过题目要求来确定结构体中需要那些元素,每次的操作都是从当前目录开始,所以需要记录当前目录的名字;一个目录下可能有多个子目录,所以可以使用map构造,如果本题不要求字典序的话可以使用unordered_map,这样会将复杂度降为O(1),使用map将在tree操作时自动按照字典序排序;由于存在cd…返回上级目录操作,所以需要记忆每一个目录的上级目录;由于需要输出sz 子目录的大小,所以要存储子树大小;由于LS输出时输出前五个和后五个目录,所以需要定义字符串数组来保存当前节点的十个侯赛;由于对于tree操作来说不记忆会导致超时,所以使用bool 型update来降低重复
- 对于结构体command来说,首先要确定操作的类型,将命令转化为数字能够方便后面的操作,对于前三种命令还需要读入后续的字符串 s ,在构造函数中可以写明
- 对于各种操作,都需要维护新目录的父节点,将父节点的update置为true,获取新创建的目录存入vector数组中;当进行undo操作的时候就将数组的最后一个值取出并删除来进行undo
#include<iostream>
#include<vector>
#include<string>
#include<map>
using namespace std;
//根据键取值,所以用map,不要求字典序用unordered_map,保证字典序
char tmps[20];//输入的字符串
struct directory
{
string name;//当前目录的名字
map<string,directory* > children;//用指针避免重复构造浪费内存
directory* parent;//为了cd..返回上级目录
int subtreesize;//以备输出子树大小
directory (string name,directory* parent)//构造函数,谁生成谁
{
this->name=name;
this->parent=parent;
this->subtreesize=1;
}
vector<string>tendescendants;//保存当前节点十个后代
bool update;
//向上维护子树大小
public:
void maintain(int delta)
{
update = true;
subtreesize +=delta;
if(parent!=nullptr)
parent->maintain(delta);
}
void tree()
{
if(subtreesize==1) printf("EMPTY\n");
else if(subtreesize<=10)
{
if(this->update)
{
tendescendants.clear();
treeall(tendescendants);
this->update=false;
}
for(int i=0;i<subtreesize;i++)
printf("%s\n",tendescendants.at(i).c_str());
}
else
{
if(this->update)
{
tendescendants.clear();
treefirstsome(5,tendescendants);
treelastsome(5,tendescendants);
this->update=false;
}
for(int i=0;i<5;i++)
printf("%s\n",tendescendants.at(i).c_str());
printf("...\n");
for(int i=9;i>=5;i--)
printf("%s\n",tendescendants.at(i).c_str());
}
}
void sz()
{
printf("%d\n",this->subtreesize);
}
void ls()
{
int sz = children.size();
if(sz==0) printf("EMPTY\n");
else if(sz<=10)
for(auto &it :children)
printf("%s\n",it.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());
}
}
directory* getchild(string name)
{
auto it = children.find(name);
if(it==children.end())
return nullptr;
return it->second;
}
directory* mkdir(string s)
{
if(children.find(s)!=children.end())
return nullptr;
directory* ch = new directory(s,this);
children[s]=ch;
maintain(1);
return ch;
}
directory* rm(string s)
{
auto it = children.find(s);
if(it==children.end())
return nullptr;
maintain(-1*it->second->subtreesize);
children.erase(it);
return it->second;
}
directory* cd(string s)
{
if(s=="..")
return this->parent;
return getchild(s);
}
bool addchild(directory* ch)//加入子目录并返回成功与否
{
if(children.find(ch->name)!=children.end())
return false;
children[ch->name]=ch;
maintain(+ch->subtreesize);
return true;
}
private:
void treeall(vector<string>& bar)
{
bar.push_back(name);
for(auto &entry:children)
entry.second->treeall(bar);
}
void treefirstsome(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->subtreesize;
if(sts>=num){
it->second->treefirstsome(num,bar);
return;
}
else{
it->second->treefirstsome(sts,bar);
num-=sts;
}
it++;
}
}
void treelastsome(int num,vector<string>& bar)
{
int n=children.size();
auto it=children.end();
while(n--)
{
it--;
int sts=it->second->subtreesize;
if(sts>=num)
{
it->second->treelastsome(num,bar);
return;
}else{
it->second->treelastsome(sts,bar);
num-=sts;
}
}
bar.push_back(name);
}
};
//每条指令的对象都是当前目录
//有undo,所以要保存每一次的执行结果,保存到->struct command(
//使用定长容器进行存储
struct Command
{
int type;//命令类型
const string CMDNAMES[8]={"MKDIR","RM","CD","SZ","LS","TREE","UNDO"};
string arg;//命令的参数
directory *tmpdir;
Command(string s)
{
for(int i=0;i<7;i++)
if(CMDNAMES[i]==s)
{
type=i;
if(i<3)
scanf("%s",tmps),arg=tmps;//MKDIR,RM,CD参数后续读入
return;
}
}
};
void solve()//每组数据有n行命令
{
int n;scanf("%d",&n);
//对象是当前目录now
directory* now = new directory("root",nullptr);
vector<Command*> cmdlist;//UNDO
while(n--)
{
scanf("%s",tmps);
Command* cmd = new Command(tmps);
switch(cmd->type)
{
case 0://mkdir
cmd->tmpdir = now->mkdir(cmd->arg);
if(cmd->tmpdir==nullptr) printf("ERR\n");
else
{
printf("OK\n");
cmdlist.push_back(cmd);
}
break;
case 1://rm
cmd->tmpdir = now->rm(cmd->arg);
if(cmd->tmpdir==nullptr) printf("ERR\n");
else
{
printf("OK\n");
cmdlist.push_back(cmd);
}
break;
case 2://cd
{
directory* ch=now->cd(cmd->arg);
if(ch==nullptr) printf("ERR\n");
else
{
printf("OK\n");
cmd->tmpdir = now;
now=ch;
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
{
bool success = false;
while(!success && !cmdlist.empty())
{
cmd = cmdlist.back();
cmdlist.pop_back();
switch(cmd->type)
{
case 0:success = now->rm(cmd->tmpdir->name)!=nullptr;break;//undo mkdir
case 1:success = now->addchild(cmd->tmpdir);break;
case 2:now=cmd->tmpdir;success=true;break;
}
}
printf(success ? "OK\n":"ERR\n");
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
solve();
return 0;
}