SDU 程序设计思维与实践 Week9 目录管理 【树形结构、面向对象】

Directory Management

英文原版题目

中文题意

咕咕东的操作系统在上个月受到宇宙射线的影响,时不时发生故障,他受不了了,想要写一个高效易用零bug的操作系统 —— 这工程量太大了,所以他定了一个小目标,从实现一个目录管理器开始。
前些日子,东东的电脑终于因为过度收到宇宙射线的影响而宕机,无法写代码。他的好友TT正忙着在B站看猫片,另一位好友瑞神正忙着打守望先锋。现在只有你能帮助东东!
初始时,咕咕东的硬盘是空的,命令行的当前目录为根目录 root。目录管理器可以理解为要维护一棵有根树结构,每个目录的儿子必须保持字典序。
现在咕咕东可以在命令行下执行以下命令:

命令类型实现说明
MKDIR 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)的影响,撤销成功输出 “YES” 失败或者没有操作用于撤销则输出 “ERR”

输入

1、输入文件包含多组测试数据,第一行输入一个整数表示测试数据的组数 T ( T <= 20);
2、每组测试数据的第一行输入一个整数表示该组测试数据的命令总数 Q ( Q < = 1 0 5 Q<= 10^5 Q<=105);
3、每组测试数据的 2~ Q+1 行为具体的操作 (MKDIR、RM 操作总数不超过 5000);

输出

对应操作的结果

实现要求

  • 6000ms
  • 1048576 kB

一、准备阶段

  • 第1步,从程序的入口开始
#incldue<bits/stdc++.h>
using namespace std;
int main(){
	int T;cin>>T;
	while(T--)solve();
	return 0;
}
  • 第2步,构建solve函数的框架
void solve(){
	int n;cin>>n;
	while(n--){
		cin>>temps;
		...
	}
}
  • 第3步,发现需要声明一个辅助输入的字符串数组。
char temps[20];
  • 第4步,一条命令不单纯是字符串,它可能设计undo,考虑封装成Command,以便服用和拓展
while(n--){
	scanf("%s",tmps);
	Command* cmd=new Command(tmmps);
}

二、思考封装

考虑到一条命令不单有命令形式,还有命令参数,比如“MKDIR s”。我们肯定还要进行参数的分离,同类信息最好内聚,所以封装之。

  • 第5步,封装command
struct Command
{
	int type;//命令的类型
	string arg;//命令的 参数
	Command(string s) {//构造函数
		....
	}
};
  • 第6步,在Command的构造函数中,在设置命令的类型时,考虑用常量数组来使代码更加优雅。
const string CMDNAMES[7] = { "MKDIR","RM","CD","SZ","LS","TREE","UNDO" };

构造函数:

Command(string s) {//构造函数
		for(int i=0;i<7;i++)
			if (CMDNAMES[i] == s) {
				type = i;
				//MKDIR、RM、CD的参数后续读入
				if (i < 3) scanf("%s", tmps), arg = tmps;
				return;
			}
	}

三、树形结构

题目要求一个目录能够根据子目录的名字取到它的子目录。由于属于根据键取值类型,即Key-Value,所以采用map。所以要用 map<string,目录>,它可以根据 key 也就是 string 在内部进行排序。这样每次可以 log 级别复杂度取到子目录

  • 第7步,树形结构类
struct Directory
{
	string name;//当前目录的名字
	map<string, Directory*>children;//用指针是避免复制构造造成的内存浪费
	Directory* parent;//以备CD.. 返回上级目录
	int subtreeSize;//以备sz要输出子树大小		
	Directory(string name, Directory* parent) {
		this->name = name;
		this->parent = parent;
		this->subtreeSize = 1;			
	}
};

四、解题框架

  • 第8步,假设Directory类的已经设计完毕,可以直接运用。这样当真正设计Directory类的时候会使目标更加明确。
void solve(){	
	int n; cin >> n;	
	while (n--) {
		scanf("%s", tmps);		
		Command* cmd = new Command(tmps);		
		switch (cmd->type)
		{
			case 0://MKDIR
			case 1://RM
			case 2://CD	
			case 3://SZ;
			case 4://LS;
			case 5://TREE		
			case 6://undo			
		}
	cout<<endl;//题目要求不同组数据直接要输出换行
}
  • 第9步,发现每一条指令需要有一个执行对象,需要进行记录
	Directory *now = new Directory("root", nullptr);	

如此,解题框架变成了

void solve(){	
	int n; cin >> n;	
	Directory *now = new Directory("root", nullptr);
	while (n--) {
		scanf("%s", tmps);		
		Command* cmd = new Command(tmps);		
		switch (cmd->type)
		{
			case 0:now->mkdir(cmd->arg) //MKDIR
			case 1:now->rm(cmd->arg)//RM
			case 2:now->cd(cmd->arg)//CD	
			case 3:now->sz();//SZ;
			case 4:now->ls();//LS;
			case 5:now->tree();//TREE		
			case 6://undo			
		}
	cout<<endl;//题目要求不同组数据直接要输出换行
}
  • 第10步,更新Diretory结构体里面的具体实现
    在这里插入图片描述
  • 第11步,这时发现两个问题。第一,要设计“返回值”告知某条命令的执行的结果成功/失败。 第二,“UNDO”没法封装在某一个 Directory 内部,它是隶属于当前测试数据环境的。所以,对于执行成功的命令,要保存起来,以备“UNDO。
    分析UNDO命令,它必须是 MKDIR、RM、CD 三种之一,而且必须已执行成功。
    需要保存每条指令执行的执行结果,保存到 -> struct Command{…}。
	Directory* tmpDir;//记录刚刚操作涉及的目录节点
  • 第12步,输出命令执行结果,存起来成功执行的指令.
void solve(){	
	int n; cin >> n;	
	Directory *now = new Directory("root", nullptr);
	vector<Command*>cmdList;//新增加的数组存成功执行的命令以备undo
	while (n--) {
		scanf("%s", tmps);		
		Command* cmd = new Command(tmps);
		Directory * ch;
		switch (cmd->type)
		{
		case 0:case 1://MKDIR、RM
			cmd->tmpDir = cmd->type == 0 ? now->mkdir(cmd->arg) : now->rm(cmd->arg);
			if (cmd->tmpDir == nullptr) printf("ERR\n");
			else {
				printf("OK\n");
				cmdList.push_back(cmd);
			}
			break;
		case 2://CD
			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:now->sz(); break;
		case 4:now->ls(); break;
		case 5:now->tree(); break;
		//15、解决undo
		case 6://undo			
		}
	}
	cout<<endl;
}
  • 第13步,设计UNDO.
case 6://undo
			bool success = false;//undo执行成功与否
			while (!success && !cmdList.empty()) {
				cmd = cmdList.back(); cmdList.pop_back();
				switch (cmd->type)
				{
				//UNDO MKDIR  
				case 0:success = now->rm(cmd->arg) != nullptr; break;
				//UNDO RM
				case 1:success = now->addChild(cmd->tmpDir); break;
				//UNDO CD
				case 2:now = cmd->tmpDir; success = true; break;
				}
			}
			printf(success ? "OK\n" : "ERR\n");
		}

五、目录类实现细节

struct Directory
{
	string name;//当前目录的名字
	map<string, Directory*>children;//用指针是避免复制构造造成的内存浪费
	Directory* parent;//以备CD.. 返回上级目录
	int subtreeSize;//以备sz要输出子树大小
	vector<string>* tenDescendants;//保存当前节点的十个后代
	bool updated;//记录当前节点的子孙有无变动,无变动则十个后代无需更新
	Directory(string name, Directory* parent) {
		this->name = name;
		this->parent = parent;
		this->subtreeSize = 1;
		this->tenDescendants=new vector<string>;		
	}	
public:
	void maintain(int delta) {//向上维护子树大小
		updated = true;
		subtreeSize += delta;
		if (parent != nullptr)parent->maintain(delta);
	}	
	Directory* getChild(string name) {
		//取子目录并返回,不存在返回空指针
		auto 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) {
		//删除子目录并返回,删除失败返回空指针
		auto 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 sz() {
		printf("%d\n", this->subtreeSize);
	}
	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());

		}
	}

};

六、TREE功能设计

利用缓存技术。因为节点数远少于 TREE 操作数,可能会有重复询问,对于目录相同期间问过的相同问题,理应只有一次是计算过程。

  • tree()函数:
void tree() {
		if (subtreeSize == 1) printf("EMPTY\n");
		else if (subtreeSize <= 10) {			
			if (this->updated) {				
				tenDescendants->clear();				
				treeAll(tenDescendants);
				this->updated = false;
			}
		
			for (int i = 0; i < subtreeSize; i++)
				printf("%s\n", tenDescendants->at(i).c_str());
		}
		else {			
			if (this->updated) {
				tenDescendants->clear();
				treeFirstSome(5, tenDescendants);
				treeLastSome(5, tenDescendants);
				this->updated = 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 treeAll(vector<string>* bar) {		
		//更新全桶
		bar->push_back(name);
		
		for (auto &entry : children)
			entry.second->treeAll(bar);
	}
  • 更新先根序列的5个
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++;
		}
	}
  • 更新后根序列的5个
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);
	}
};

解题代码

#include<iostream>
#include<cstdio>
#include<algorithm> 
#include<map>
#include<string>
#include<vector>
using namespace std;
//3、声明一个辅助输入的字符串数组
char tmps[20];//meaning->temp string
//6、将类型封装成常量
const string CMDNAMES[7] = { "MKDIR","RM","CD","SZ","LS","TREE","UNDO" };
//7、树形结构
struct Directory
{
	string name;//当前目录的名字
	map<string, Directory*>children;//用指针是避免复制构造造成的内存浪费
	Directory* parent;//以备CD.. 返回上级目录
	int subtreeSize;//以备sz要输出子树大小
	vector<string>* tenDescendants;//保存当前节点的十个后代
	bool updated;//记录当前节点的子孙有无变动,无变动则十个后代无需更新
	Directory(string name, Directory* parent) {
		this->name = name;
		this->parent = parent;
		this->subtreeSize = 1;
		this->tenDescendants=new vector<string>;		
	}	
public:
	void maintain(int delta) {//向上维护子树大小
		updated = true;
		subtreeSize += delta;
		if (parent != nullptr)parent->maintain(delta);
	}
	void tree() {
		if (subtreeSize == 1) printf("EMPTY\n");
		else if (subtreeSize <= 10) {			
			if (this->updated) {				
				tenDescendants->clear();				
				treeAll(tenDescendants);
				this->updated = false;
			}
		
			for (int i = 0; i < subtreeSize; i++)
				printf("%s\n", tenDescendants->at(i).c_str());
		}
		else {			
			if (this->updated) {
				tenDescendants->clear();
				treeFirstSome(5, tenDescendants);
				treeLastSome(5, tenDescendants);
				this->updated = 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());
		}
	}	
	//12、在结构体类写各种具体的实现
	Directory* getChild(string name) {
		//取子目录并返回,不存在返回空指针
		auto 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) {
		//删除子目录并返回,删除失败返回空指针
		auto 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 sz() {
		printf("%d\n", this->subtreeSize);
	}
	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());

		}
	}
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);
	}
};
//5、封装command
struct Command
{
	int type;//命令的类型
	string arg;//命令的 参数
	Command(string s) {//构造函数
		for(int i=0;i<7;i++)
			if (CMDNAMES[i] == s) {
				type = i;
				//MKDIR、RM、CD的参数后续读入
				if (i < 3) scanf("%s", tmps), arg = tmps;
				return;
			}
	}
	//13、修改command 需要保存每条指令的执行结果
	Directory* tmpDir;//记录刚刚操作涉及的目录节点

};

//14、输出命令执行结果,存起来成功执行的命令
void solve(){
	//2、每组数据有m行命令
	int n; cin >> n;
	//10、这个对象就是当前目录now
	Directory *now = new Directory("root", nullptr);
	vector<Command*>cmdList;//新增加的数组存成功执行的命令以备undo
	while (n--) {
		scanf("%s", tmps);
		//4、一条命令并不单纯是字符串,它可能涉及undo,考虑封装成Command,以便复用/拓展
		Command* cmd = new Command(tmps);
		Directory * ch;
		switch (cmd->type)
		{//9、要开始具体写实现的时候,对于每条命令要有一个执行对象
		case 0:case 1://MKDIR、RM
			cmd->tmpDir = cmd->type == 0 ? now->mkdir(cmd->arg) : now->rm(cmd->arg);
			if (cmd->tmpDir == nullptr) printf("ERR\n");
			else {
				printf("OK\n");
				cmdList.push_back(cmd);
			}
			break;
		case 2://CD
			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:now->sz(); break;
		case 4:now->ls(); break;
		case 5:now->tree(); break;
		//15、解决undo
		case 6://undo
			bool success = false;//undo执行成功与否
			while (!success && !cmdList.empty()) {
				cmd = cmdList.back(); cmdList.pop_back();
				switch (cmd->type)
				{
				//UNDO MKDIR  !!!bug点 
				case 0:success = now->rm(cmd->arg) != nullptr; break;
				//UNDO RM
				case 1:success = now->addChild(cmd->tmpDir); break;
				//UNDO CD
				case 2:now = cmd->tmpDir; success = true; break;
				}
			}
			printf(success ? "OK\n" : "ERR\n");
		}
	}
	cout<<endl;
}
int main(){
	//1、测试有T组数据
	int T;cin>>T;
	while(T--) solve(); 
	return 0; 
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值