hdu 1413 文件系统

来源:互联网 发布:淘宝开店加盟靠谱吗 编辑:程序博客网 时间:2024/06/05 23:43
 此题是一个模拟题,做模拟题,分清条件就可以了。本题我是用c++做的,这样我们可以用string要简便一些。加上vector、map,这样本题就简化多了。具体实现看代码:
#include<iostream>
#include<vector>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>

using namespace std;

struct command{
   vector<string> dir;//当前子目录
vector<string>file;//当前目录下的文件
map<string,int> num;//对应当前子目录的编号
vector<command *>next;//指向子目录
command *pre;//指向当前目录的上一级
};

command *root;

int Judge(stringk,vector<string> p){//执行查找
vector<string>::iteratorit;
it=find(p.begin(),p.end(),k);
return it!=p.end();
}

int DElete(string p,string q,command *t){//执行删除和判断
vector<string>::iteratorit;
int x,i;
if(q=="file"){
it=find(t->file.begin(),t->file.end(),p);
t->file.erase(it);
}
else{
x=t->num[p];
for(i=0;i<t->next.size()&&x>0;i++)x--;
if(t->next[i]->dir.size() ||t->next[i]->file.size())
return 0;
it=find(t->dir.begin(),t->dir.end(),p);
t->dir.erase(it);
}
return 1;
}

void make(){
    string p,q;
root=new command();
command *h=root,*t;
int x;
while(cin>>p>>q){
if(p=="CD"){//进入一个子目录
           t=h;
if(q==".." || q=="\\"){
cout<<"success"<<endl;
if(q=="..")
h=t->pre;
else
h=root;
}
else if(Judge(q,t->dir)){
cout<<"success"<<endl;
x=t->num[q];
             t=t->next[x];
h=t;
}
else{
cout<<"no suchdirectory"<<endl;
}
}
else if(p=="MD"){//创建一个子目录
t=h;
if(q==".." || Judge(q,t->dir)){
cout<<"directory alreadyexist"<<endl;
}
else{
             cout<<"success"<<endl;
t->dir.push_back(q);
t->num[q]=t->next.size();
command *newafile=new command();
t->next.push_back(newafile);
t=newafile;
t->pre=h;
}
}
else if(p=="RD"){//删除一个子目录
t=h;
if(Judge(q,t->dir)){
if(DElete(q,"dir",t)){
cout<<"success"<<endl;
}
else
cout<<"can not delete thedirectory"<<endl;
}
else{
cout<<"can not delete thedirectory"<<endl;
}
}
else if(p=="CREATE"){//创建一个文件
t=h;
if(Judge(q,t->file)){
               cout<<"file alreadyexist"<<endl;
}
else
{
t->file.push_back(q);
cout<<"success"<<endl;
}
}
else if(p=="DELETE"){//删除一个文件
t=h;
if(Judge(q,t->file)){
DElete(q,"file",t);
cout<<"success"<<endl;
}
else
             cout<<"no suchfile"<<endl;
}
}
}

int main(){
    make();
return 0;
}
原创粉丝点击