问题描述:
题面

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

hint

思路:
这道题因为助教课上已经讲过,而且基本一致只有一些地方出现了bug稍微改一下就好了,所以不再说解题思路了。

#include <iostream>
#include<string.h>
#include<vector>
#include<string>
#include<map>
using namespace std;
string tm;struct Directory
{string name;map<string, Directory*> children;Directory* parent;int treesize;vector<string> tenD;bool update;Directory(string xx, Directory* yy){this->name = xx;this->parent = yy;this->treesize = 1;}
public:void maintain(int x){update = true;treesize = treesize + x;if (parent != NULL)parent->maintain(x);}bool addchild(Directory* ch){if (children.find(ch->name) != children.end())return false;children[ch->name] = ch;maintain(+ch->treesize);return true;}Directory* getchild(string name){auto it = children.find(name);if (it == children.end())return NULL;else return it->second;}Directory* mkdir(string name){if (children.find(name) != children.end()){return NULL;}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 NULL;Directory* iter = it->second;maintain(-1 * it->second->treesize);it = children.erase(it);return iter;}Directory* cd(string name){if (name == ".."){return this->parent;}return getchild(name);}void sz(){printf("%d\n", this->treesize);}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 (treesize == 1)printf("EMPTY\n");else if (treesize <= 10){if (this->update){tenD.clear();treeall(&tenD);this->update = false;}for (int i = 0; i < tenD.size(); i++)printf("%s\n", tenD.at(i).c_str());}else{if (this->update){tenD.clear();treef(5, &tenD);treel(5, &tenD);this->update = false;}for (int i = 0; i < 5; i++)printf("%s\n", tenD.at(i).c_str());printf("...\n");for (int i = 9; i >= 5; i--)printf("%s\n", tenD.at(i).c_str());}}
private:void treeall(vector<string>* bar){bar->push_back(name);for (auto& entry : children)entry.second->treeall(bar);}void treef(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->treesize;if (sts >= num) {it->second->treef(num, bar);return;}else{it->second->treef(sts, bar);num -= sts;}it++;}}void treel(int num, vector<string>* bar){int n = children.size();auto it = children.end();while (n--){it--;int sts = it->second->treesize;if (sts >= num) {it->second->treel(num, bar);return;}else{it->second->treel(sts, bar);num -= sts;}}bar->push_back(name);}
};
struct Command
{const string vis[7] = { "MKDIR","RM","CD","SZ","LS","TREE","UNDO" };int lei;Directory* t;string a;Command(string x){for (int i = 0; i < 7; i++){if (vis[i] == x){lei = i;break;}}if (lei < 3){cin >> tm;a = tm;}}
};
void cs()
{int n = 0; cin >> n;Directory* now = new Directory("root", NULL);vector<Command*> cmdlist;while (n--){cin >> tm;Command* cmd = new Command(tm);Directory* cs;int cl = cmd->lei;bool success = false;switch (cl){case 0:cs = now->mkdir(cmd->a);if (cs == NULL)printf("ERR\n");else{cmd->t = cs;printf("OK\n");cmdlist.push_back(cmd);}break;case 1:cs = now->rm(cmd->a);if (cs == NULL)printf("ERR\n");else{printf("OK\n");cmd->t = cs;cmdlist.push_back(cmd);}break;case 2:cs = now->cd(cmd->a);if (cs == NULL)printf("ERR\n");else{printf("OK\n");cmd->t = now;now = cs;cmdlist.push_back(cmd);}break;case 3:now->sz();break;case 4:now->ls();break;case 5:now->tree();break;case 6:while (!success && cmdlist.size()){cmd = cmdlist.back(); cmdlist.pop_back();switch (cmd->lei){case 0:success = now->rm(cmd->a) != NULL; break;case 1:success = now->addchild(cmd->t);  break;case 2:now = cmd->t; success = true; break;}}printf(success ? "OK\n" : "ERR\n");break;}}
}int main(int argc, char** argv)
{int T; cin >> T;while (T--)cs();return 0;
}

week 9 A目录相关推荐

  1. http://blog.csdn.net/neiloid/article/details/7037093#

    1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器): androi ...

  2. zip压缩多个文件,解压时不包含目录层级

    假设我们有个目录叫 dev,dev中有很多文件,我们想要将dev中的文件打包,名字可能叫dev.zip,但当我们解压的时候,不想要解压生成一个dev目录,想要直接解压在当前目录,这样如何压缩呢? # ...

  3. 将文件上传至ftp服务器,FTP文件上传工具类,将文件上传至服务器指定目录

    将文件上传至ftp服务器,传入File对象,将文件上传至ftp服务器 需要配置修改的点: 1. 服务器ip端口(服务器ip 端口22/21). 2. 服务器账号密码(服务器登录用户名密码). 3. 上 ...

  4. Go 学习笔记(84)— Go 项目目录结构

    1. 目录规范 一个好的目录结构至少要满足以下几个要求. 命名清晰:目录命名要清晰.简洁,不要太长,也不要太短,目录名要能清晰地表达出该目录实现的功能,并且目录名最好用单数.一方面是因为单数足以说明这 ...

  5. GCC 连接器、链接标准库 gcc -l、链接手动创建库(指定目录的库 gcc -L)

    1. 链接器 链接器把多个二进制的目标文件(object file)链接成一个单独的可执行文件. 在链接过程中,它必须把符号(变量名.函数名等一些列标识符)用对应的数据的内存地址(变量地址.函数地址等 ...

  6. Go 语言同一个包内函数调用、包名和实际路径最后一个目录不一致问题

    以下代码的 GOPATH 路径为 "/home/wohu/GoCode" 1. 同一个包内的函数可以相互调用 代码结构如下: wohu@wohu:~/GoCode/src$ tre ...

  7. Linux shell 学习笔记(1)— 文件和目录(查看、创建、复制、软硬链接、重命名及删除操作)

    1. 启动 shell /etc/passwd 文件包含了所有系统用户账户列表以及每个用户的基本配置信息: christine:x:501:501:Christine Bresnahan:/home/ ...

  8. Python 标准库之 os (获取当前目录、读取/设置环境变量、重命名文件、运行shell命令、创建/删除/查看目录文件、判断目录/文件/存在、获取绝对路径、获取文件名、获取换行符、获取路径分隔符)

    1. os与sys模块的官方解释如下: os This module provides a portable way of using operating system dependent funct ...

  9. Docker 入门系列(4)- Docker 数据管理(挂载目录、挂载文件、数据卷挂载、数据卷共享、数据卷删除、数据卷容器备份和恢复)

    基于底层存储实现,Docker 提供了三种适用于不同场景的文件系统挂载方式:Bind Mount.Volume 和 Tmpfs Mount. Bind Mount 能够直接将宿主操作系统中的目录和文件 ...

  10. 解决LC_ALL: 无法改变区域选项 (UTF-8): 没有那个文件或目录的问题

    问题: -bash: 警告:setlocale: LC_ALL: 无法改变区域选项 (UTF-8): 没有那个文件或目录 -bash: 警告:setlocale: LC_ALL: 无法改变区域选项 ( ...

最新文章

  1. MySQL --- 计算指定日期为当月的第几周
  2. 天锋w2019_什么样的商务手机才显得高端?这款天锋W2019可能适合你
  3. 六安瓜片在51CTO【礼树迎蛇 红满社区】
  4. WiFi曝出安全漏洞几近“裸奔”:运营商能借机收割一波红利吗?
  5. Centos7安装整合Apache+PHP,安装nginx后nginx无法解析.php文件
  6. py文件编译成pyc文件
  7. loadrunner11 post请求接口压力测试并生成报告
  8. 以迅雷链为首的中国区块链,用技术赋能行业实现突围
  9. 反向题在测试问卷信效度_问卷前测除了信效度,你还需知道...
  10. Calendar根据输入的年份和周数计算该周的开始日期和结束日期
  11. iPad 上能播放局域网高清片源影音播放利器XBMC使用体会
  12. idea 报错Output directory is not specified错误
  13. Android 触摸屏Event上报操作
  14. Excel的公式:公式基本使用、单元格地址引用、错误值利用、追踪公式利用与追踪错误
  15. ios8在通用设置中文键盘无效的解决方法(中文键盘设置)
  16. 网络基础——牛客网刷题第四波
  17. 关于项目管理的通俗讲解
  18. jquery 图片转为base64
  19. 实现人脸磨皮算法---OpenCV-Python开发指南(58)
  20. RTMP协议推流,助力视频数据轻松上云

热门文章

  1. 盘点6个主流的数据分析工具,及优缺点对比
  2. html搜索栏热搜效果,CSS3实战开发:百度新闻热搜词特效实战开发_html/css_WEB-ITnose...
  3. TEB轨迹优化算法-代码解析与参数建议
  4. 如何完成一次 git pr
  5. jstack排查死锁问题
  6. python docx转换成txt文本
  7. Visual Haze Removal by a Unified GenerativeAdversarial Network(基于生成式对抗网络的图像去雾IEEE2019)
  8. 逆向某视频app(一)
  9. Ubuntu Desktop gedit
  10. gthub获得star指南