咕咕东的目录管理器

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

样例:
输入:
在这里插入图片描述

输出:
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

解题思路:大模拟,这个题信息量很大,想要作对得思路和结构都很清晰;首先构建两个结构体,其中一个储存目录的信息,另外一个储存命令的信息,并且构造一个数组储存你用过的命令,方undo;这里最好使用map来进行边的存储,如果遇见undo,前一个命令是cd的话,就可以利用那个你的命令存储的数组直接得到答案;如果是rm的话,就恢复map;mkdir就去掉map;然后到了那个tree,这个挺复杂的;可以采用记录更新的方法,如果一个结构体内被标记为没有更新,就要对其进行更新;如果标记为更新,就可以直接拿来用,把要求的信息放进一个数组中,直接输出。

代码:

#include<bits/stdc++.h>
using namespace std;
string tmps;
struct Directory//结构体记录文件
{
    string name;
    map<string,Directory*> children;//边
    Directory* parent=NULL;//父亲
    int subtreesize;//大小
    vector<string>* tendescendants=new vector<string>;//记录tree的数组
    bool updated;//是否要进行更新
    Directory(string name,Directory* parent)
    {
        this->name=name;
        this->parent=parent;
        this->subtreesize=1;
    }
    void treeAll(vector<string>* bar)//3个更新函数
    {
        bar->push_back(name);
        map<string,Directory*>::iterator it;
        for(it=children.begin();it!=children.end();it++)
        {
            it->second->treeAll(bar);
        }
    }
    void treefirstsome(int num,vector<string>* bar)
    {
        bar->push_back(name);
        if(--num==0)return;
        int n=children.size();
        map<string,Directory*>::iterator 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();
        map<string,Directory*>::iterator 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);
    }
    Directory* getchild(string name)//得到孩子
    {
        if(children.find(name)==children.end())
            return NULL;
        return children.find(name)->second;
    }
    Directory* mkdir(string name)//新建一条边
    {
        if(children.find(name)!=children.end())
        {
            return NULL;
        }
        Directory* ch=new Directory(name,this);//赋值
        children[name]=ch;//儿子出现
        maintain(+1);//+1
        return ch;
    }
    Directory* rm(string name)//删除
    {
        map<string,Directory*>::iterator it=children.find(name);
        if(it==children.end())
        {
            return NULL;
        }
        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)//undo操作
    {
        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!=NULL)
        {
            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)
        {
            map<string,Directory*>::iterator it;
            for(it=children.begin();it!=children.end();it++)
            {
                cout<<it->first<<endl;
            }
        }else
        {
            map<string,Directory*>::iterator it=children.begin();
            for(int i=0;i<5;i++,it++)cout<<it->first<<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<<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;i<subtreesize;i++)cout<<tendescendants->at(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->at(i)<<endl;
            cout<<"..."<<endl;
            for(int i=9;i>=5;i--)cout<<tendescendants->at(i)<<endl;
        }
    }
};
struct command//命令结构体
{
    const string cmdnames[7]={"MKDIR","RM","CD","SZ","LS","TREE","UNDO"};
    int type;
    string arg;
    Directory* tmpDir=NULL;
    command(string s)
    {
        for(int i=0;i<7;i++)
        {
            if(cmdnames[i]==s)
            {
                type=i;
                if(i<3)
                    cin>>tmps;
                    arg=tmps;
                break;
            }
        }
    }
};
int main()
{
    int t;
    cin>>t;
    int flag=1;
    while(t--)
    {
        int n;
        cin>>n;
        Directory* now=new Directory("root",NULL);
        vector<command*> cmdList;
        while(n--)
        {
            cin>>tmps;
            command* cmd=new command(tmps);
            switch(cmd->type)
            {
                case 0:
                    {
                        cmd->tmpDir=(cmd->type==0?now->mkdir(cmd->arg):now->rm(cmd->arg));
                        if(cmd->tmpDir==NULL)cout<<"ERR"<<endl;
                        else{
                            cout<<"OK"<<endl;
                            cmdList.push_back(cmd);
                        }
                    }
                    break;
                case 1:
                    {
                        cmd->tmpDir=(cmd->type==0?now->mkdir(cmd->arg):now->rm(cmd->arg));
                        if(cmd->tmpDir==NULL)cout<<"ERR"<<endl;
                        else{
                            cout<<"OK"<<endl;
                            cmdList.push_back(cmd);
                        }
                    }
                    break;
                case 2:
                    {
                        Directory* ch=now->cd(cmd->arg);
                        if(ch==NULL)cout<<"ERR"<<endl;
                        else{
                            cout<<"OK"<<endl;
                            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;
                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)!=NULL);break;
                                case 1: success=(now->addchild(cmd->tmpDir));break;
                                case 2: now=cmd->tmpDir;success=true;break;
                            }
                        }
                        cout<<(success?"OK":"ERR")<<endl;
                    }
            }
        }
    }
    return 0;
}
发布了34 篇原创文章 · 获赞 0 · 访问量 857

猜你喜欢

转载自blog.csdn.net/qq_43653717/article/details/105691852
今日推荐