Week9 作业

A - 咕咕东的目录管理器题目咕咕东的雪梨电脑的操作系统在上个月受到宇宙射线的影响,时不时发生故障,他受不了了,想要写一个高效易用零bug的操作系统 —— 这工程量太大了,所以他定了一个小目标,从实现一个目录管理器开始。前些日子,东东的电脑终于因为过度收到宇宙射线的影响而宕机,无法写代码。他的好友TT正忙着在B站看猫片,另一位好友瑞神正忙着打守望先锋。现在只有你能帮助东东!初始时,咕咕东的硬...
摘要由CSDN通过智能技术生成

A - 咕咕东的目录管理器

题目

咕咕东的雪梨电脑的操作系统在上个月受到宇宙射线的影响,时不时发生故障,他受不了了,想要写一个高效易用零bug的操作系统 —— 这工程量太大了,所以他定了一个小目标,从实现一个目录管理器开始。前些日子,东东的电脑终于因为过度收到宇宙射线的影响而宕机,无法写代码。他的好友TT正忙着在B站看猫片,另一位好友瑞神正忙着打守望先锋。现在只有你能帮助东东!

初始时,咕咕东的硬盘是空的,命令行的当前目录为根目录 root。

目录管理器可以理解为要维护一棵有根树结构,每个目录的儿子必须保持字典序。
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

输入

输入文件包含多组测试数据,第一行输入一个整数表示测试数据的组数 T (T <= 20);

每组测试数据的第一行输入一个整数表示该组测试数据的命令总数 Q (Q <= 1e5);

每组测试数据的 2 ~ Q+1 行为具体的操作 (MKDIR、RM 操作总数不超过 5000);

面对数据范围你要思考的是他们代表的 “命令” 执行的最大可接受复杂度,只有这样你才能知道你需要设计的是怎样复杂度的系统。

输出

每组测试数据的输出结果间需要输出一行空行。注意大小写敏感。

时空限制

Time limit 6000 ms

Memory limit 1048576 kB

样例输入

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

做法

非常非常非常复杂的模拟题,参照讲课PPT做法。首先从程序入口开始;然后考虑封装树形结构;接着考虑解题框架;接下来是细节处理,逐一实现各具体函数;最后实现比较复杂的TREE命令。

代码

#include<iostream>
#include<vector>
#include<map>
using namespace std;
char tmps[20];

struct Directory
{
   
	string name;
	map<string,Directory*> children;
	Directory* parent;
	int subtreeSize;
	Directory(string name,Directory* parent)
	{
   
		this->name=name;
		this->parent=parent;
		this->subtreeSize=1;
		updated=true;
		tenDescendants=new vector<string>;
	}
	vector<string>* tenDescendants;
	bool updated;
public:	
	Directory* getChild(string name)
	{
   
		map<string,Directory*>::iterator it=children.find(name);
		if(it==children.end()) return nullptr;
		return it->second;
	}
	Directory* mkdir(string name)
	{
   
		if(children.find(name)!=children.end())
		 return nullptr;
		Directory* ch=new Directory(name,this);
		children[name]=ch;
		maintain(1);
		return ch; 
	}
	Directory* rm(string name)
	{
   
		map<string,Directory*>::iterator it=children.find(name);
		if(it==children.end())
		 return nullptr;
		maintain(-1 * it->second->subtreeSize);
		children.erase(it);
		return it->second;
	}
	Directory* cd(string name)
	{
   
		if(".."==name)
		 return this->parent;
		return getChild(name);
	}
	bool addChild(Directory* ch)
	{
   
		if(children.find(ch->name)!=children.end())
		 return false;
		children[ch->name]=ch;
		maintain(+ch->subtreeSize);
		return true;
	}
	void maintain(int delta)
	{
   
		updated=true;
		subtreeSize+=delta;
		if(parent!=nullptr)
		 parent->maintain(delta);
	}
	void sz()
	{
   
		cout<<this->subtreeSize<<endl;
	}
	void ls()
	{
   
		int sz=children.size();
		if(sz==0) cout<<"EMPTY"<<endl;
		else if(sz<=10)
		//for(auto &entry : children) cout<<entry.first.c_str()<<endl;
		{
   
			map<string,Directory*>::iterator it=children.begin();
			for(it;it!=children.end();it++)
			  cout<<it->first.c_str()<<endl;
		}
		else
		{
   
			map<string,Directory*>::iterator it=children.begin();
			for(int i=0;i<5;i++,it++)
			 cout<<it->first.c_str()<<endl;
			cout<<"..."<<endl;
			it=children.end();
			for(int i=0;i<5;i++) it--;
			for(int i=0;i<5;i++,it++) cout<<it->first.c_str()<<endl;
		}
	}
	void tree()
	{
   
		if(subtreeSize==1) cout<<"EMPTY"<<endl;
		else if(subtreeSize<=10)
		{
   
			if(this->updated)
			{
   
				tenDescendants->clear();
				treeAll(tenDescendants);
				this->updated=false;
			}
			for(int i
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值