#include <iostream>
#include <string>
#include <stdlib.h>
#include <vector>
using namespace std;
class Dir{
public:
string name;
Dir *parent;
Dir *self;
Dir(){
name = "";
parent = NULL;
self = NULL;
}
};
class File{
public:
string name;
Dir *parent;
File *self;
File(){
name = "";
parent = NULL;
self = NULL;
}
};
Dir allD[10010];
File allF[10010];
int Dn;
int Fn;
vector<Dir*> allDirs;
vector<File*> allFiles;
bool CreateFile(string par,string name){
bool canFind = false;
vector<Dir*>::iterator it = allDirs.begin();
while(it != allDirs.end()){
if((*it)->name ==par){
canFind = true;
break;
}
it++;
}
if(!canFind)
return false;
canFind = false;
for(int i = 0 ; i < allFiles.size();i++){
if(allFiles[i]-> name == name){
canFind = true;
break;
}
}
if(canFind)
return false;
allF[Fn].name = name;
allF[Fn].parent = *it;
allF[Fn].self = &allF[Fn];
allFiles.push_back(allF[Fn++].self);
}
bool CreateDir(string par,string name){
bool canFind = false;
vector<Dir*>::iterator it = allDirs.begin();
while(it != allDirs.end()){
if((*it)->name ==par){
canFind = true;
break;
}
it++;
}
if(!canFind)
return false;
canFind = false;
for(int i = 0 ; i < allDirs.size();i++){
if(allDirs[i]-> name == name){
canFind = true;
break;
}
}
if(canFind)
return false;
allD[Dn].name = name;
allD[Dn].parent = *it;
allD[Dn].self = &allD[Dn];
allDirs.push_back(allD[Dn++].self);
}
bool DeleteFile(string name){
if(allFiles.size() ==0 )
return false;
vector<File*>::iterator it = allFiles.begin();
bool canFind = false;
while(it != allFiles.end()){
if((*it)->name == name){
canFind = true;
break;
}
it++;
}
if(canFind){
it = allFiles.erase(it);
return true;
}
else
return false;
}
bool DeleteDir(string name){
if(allDirs.size() ==0 )
return false;
vector<Dir*>::iterator it = allDirs.begin();
bool canFind = false;
while(it != allDirs.end()){
if((*it)->name == name){
canFind = true;
break;
}
it++;
}
if(canFind){
vector<File*>::iterator it = allFiles.begin();
while(it != allFiles.end()){
if((*it)->parent->name == name){
it = allFiles.erase(it);
}else{
it++;
}
}
for(int i = 0 ; i < allDirs.size();i++){
if(allDirs[i]->parent->name == name)
DeleteDir(allDirs[i]->name); //递归的删除目录
}
return true;
}
else
return false;
}
int CountFile(string name){
vector<Dir*>::iterator it = allDirs.begin();
while(it != allDirs.end()){
if((*it)->name == name){
break;
}
it++;
}
if(it == allDirs.end())
return 0;
int counts = 0;
for(int i = 0 ; i < allFiles.size();i++){
if(allFiles[i]->parent->name == name){
counts ++;
}
}
for(int i = 0 ; i < allDirs.size();i++){
if(allDirs[i]->name != "root" && allDirs[i]->parent->name == name){//root 太坑。。。
counts += CountFile(allDirs[i]->name); //同样,递归的计算文件数量。
}
}
return counts;
}
bool MoveDir(string SrcDirName,string DestParentDirName){
vector<Dir*>::iterator itS = allDirs.begin();
vector<Dir*>::iterator itD = allDirs.begin();
if( DestParentDirName == SrcDirName)
return false;
while(itS != allDirs.end()){
if((*itS)->name == SrcDirName)
break;
itS++;
}
while(itD != allDirs.end()){
if((*itD)->name == DestParentDirName)
break;
itD++;
}
if(itS == allDirs.end() || itD == allDirs.end())
return false;
if((*itS) ->parent->name ==(* itD)->name)
return false;
Dir *parent = *itD;
while(parent->name != "root"){//将目标目录向上遍历,看是否有父目录为移动目录,如果有,就说明目标目录为移动目录的子目录
parent = parent->parent;
if(parent->name == SrcDirName)
return false;
}
(*itS)->parent = *itD;
return true;
}
void init(){
allDirs.clear();
allFiles.clear();
Dn = 0;
Fn = 0;
allD[Dn].name = "root";
allD[Dn].parent = NULL;
allD[Dn].self = &allD[Dn];
allDirs.push_back(allD[Dn++].self);
}
void main(){
init();
string command,buff;
int site = 0;
while(true){
getline(cin,command);
//测试用例保证输入命令合法,如测试用例保证参数个数正确。
site = command.find(" ");
buff = command.substr(0,site);
command = command.substr(site+1);
if(buff == "CreateDir"){
site = command.find(" ");
buff = command.substr(0,site);
command = command.substr(site+1);
if(!CreateDir(buff,command))
cout<<"CreateDir false"<<endl;
}else if(buff == "DeleteDir"){
DeleteDir(command) ;
}else if(buff == "MoveDir"){
site = command.find(" ");
buff = command.substr(0,site);
command = command.substr(site+1);
if(!MoveDir(buff,command))
cout<<"MoveDir false"<<endl;
}else if(buff == "CreateFile"){
site = command.find(" ");
buff = command.substr(0,site);
command = command.substr(site+1);
if(!CreateFile(buff,command))
cout<<"CreateFile false"<<endl;
}else if(buff == "DeleteFile"){
DeleteFile(command) ;
}else if(buff == "GetFileNum"){
cout<<CountFile(command)<<endl ;
}else if(buff=="Clear"){
init();
}
}
}