目录管理器 模拟—c++

题面

咕咕东的雪梨电脑的操作系统在上个月受到宇宙射线的影响,时不时发生故障,他受不了了,想要写一个高效易用零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”

输入:
输入文件包含多组测试数据,第一行输入一个整数表示测试数据的组数 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

思路:

构造两个结构体,一个用来存放指令Command,一个用来存放目录的信息Directory。
MKDIR: 新建目录:先检查当前目录下是否已经存在要新建的目录,若没有,则新建一个,并更新这个目录所属的目录的size。
RM: 删除目录:基本上和新建目录是相反的操作。
CD: 进入目录:若是"…",则要进入上一级目录,否则查找目录是否存在于当前目录下,并进入那个目录。
SZ: 输出当前目录大小:只要直接输出即可
LS: 输出当前直接子目录:看直接子目录的孩子数量,若<=10,则直接输出即可,若>10,则是另一种情况。
TREE: 输出子树:为了节约时间,对每个结点实现懒更新,对每个结点都有一个pre数组和bck数组用来存储前序遍历的前几个点,和后序的几个点。当节点不需要更新时,速度就会非常快。
所有的操作指令都放在一个vector数组里,并记录是哪个目录到哪个目录进行的操作,便于回溯。
UNDO: 撤回,查看存储指令的数组里末尾是什么指令,针对那个指令,进行相应的撤回。

代码:

#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
const string COMMAND[7] = {"MKDIR", "RM", "CD", "SZ", "LS", "TREE", "UNDO"};
struct Directory
{
		string name;
		map<string, Directory *> children;
		Directory *parent;
		int subtreeSize;
		bool updated;
		vector<string> tenDescendants;
		Directory(string name, Directory *parent)
		{
			this->parent = parent;
			this->name = name;
			this->subtreeSize = 1;
		}
	public:
		Directory *getchild(string name)
		{
			auto it = children.find(name);
			if (it == children.end())
				return NULL;
			return it->second;
		}
		Directory *mkdir(string name)
		{
			if (children.find(name) != children.end())
				return NULL;
			else
			{
				Directory *tmp = new Directory(name, this);
				children[name] = tmp;
				maintain(1);
				return tmp;
			}

		}
		Directory *rm(string name)
		{
			auto it = children.find(name);
			if (it == children.end())
				return NULL;
			else
			{
				Directory* iter = it->second;
				maintain(-1 * it->second->subtreeSize);
				it = children.erase(it);
				return iter;
			}

		}
		Directory *cd(string name)
		{
			if (".." == name)
				return this->parent;
			return getchild(name);
		}
		bool addchild(Directory *ac)
		{
			if (children.find(ac->name) != children.end())
				return false;

			children[ac->name] = ac;
			maintain(+ac->subtreeSize);
			return true;
		}
		void maintain(int delta)
		{
			updated = true;
			subtreeSize += delta;
			if (parent != NULL)
				parent->maintain(delta);
		}
		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)
					cout << entry.first << endl;
			}
			else
			{
				auto it = children.begin();
				for (int i = 0; i < 5; i++,it++)
					cout << it->first << endl;
				printf("...\n");
				it = children.end();
				for (int i = 0; i < 5; i++)
					it--;
				for (int i = 0; i < 5; i++,it++)
					cout << it->first << endl;
			}
		}
		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++)
					cout << tenDescendants[i] << endl;
			}
			else
			{
				if(this->updated)
				{
					tenDescendants.clear();
					treeFirstSome(5, &tenDescendants);
					treeLastSome(5, &tenDescendants);
					this->updated = false;
				}
				for (int i = 0; i < 5; i++)
					cout << tenDescendants[i] << endl;
				printf("...\n");
				for (int i = 9; i >= 5; i--)
					cout << tenDescendants[i] << endl;
			}
		}

	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);

		}
};

struct Command
{
	int type;
	string arg;
	Directory *tmpDir;
	Command(string &s)
	{
		for (int i = 0; i < 7; i++)
		{
			if (s == COMMAND[i])
			{
				this->type = i;
				if (i < 3)
					cin >>arg;
				return;
			}
		}
	}
};
void solve()
{
	int n;
	cin>>n;
	Directory *now = new Directory("root", NULL);
	vector<Command *> cmdList;
	string s;
	while (n--)
	{
		cin >> s;
		Command *cmd = new Command(s);
		switch (cmd->type)
		{
			case 0:
			{
				cmd->tmpDir = now->mkdir(cmd->arg);
				if (cmd->tmpDir == NULL)
					printf("ERR\n");
				else
				{
					printf("OK\n");
					cmdList.push_back(cmd);
				}
			}
			break;
			case 1:
			{
				cmd->tmpDir = now->rm(cmd->arg);
				if (cmd->tmpDir == NULL)
					printf("ERR\n");
				else
				{
					printf("OK\n");
					cmdList.push_back(cmd);
				}
			}
			break;
			case 2:
			{
				Directory *cd = now->cd(cmd->arg);
				if (cd == NULL)
				{
					printf("ERR\n");
				}
				else
				{
					printf("OK\n");
					cmd->tmpDir = now;
					now = cd;
					cmdList.push_back(cmd);
				}
			}
			break;
			case 3:
				now->sz();
				break;
			case 4:
				now->ls();
				break;
			case 5:
				now->tree();
				break;
			case 6:
			{
				bool success = false;
				while (!success && !cmdList.empty())
				{
					cmd = cmdList.back();
					cmdList.pop_back();
					switch (cmd->type)
					{
						case 0:
						{
							success = now->rm(cmd->arg)!=NULL;
						}
						break;
						case 1:
						{
							success = now->addchild(cmd->tmpDir);
						}
						break;
						case 2:
						{
							now = cmd->tmpDir;
							success = true;
						}
						break;
					}
				}
				printf(success ? "OK\n" : "ERR\n");
			}
			break;
		}
	}
}
int main()
{

	int T;
	scanf("%d", &T);
	while (T--)
	{
		solve();
	}
	return 0;
}

总结:这题真难,真不是我这种菜鸟能写出来的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值