#include<iostream>
#include<map>
#include<list>
#include<string>
using namespace std;
struct Tman{
string name;//本身的名字
Tman *f;//父节点
list<Tman *> s;//子节点
};
map<string,Tman *> hash;//表示hash是一个一字符串为标识的数组:hash[string] = Tman *;
Tman *root;
void output(int dev,Tman *now){
if(now==NULL)
return ;
for(int i=1;i<=dev;++i)
cout<<'+';
cout<<now->name<<endl;
for(list<Tman *>::iterator j=now->s.begin();j!=now->s.end();++j){
output(dev+1,*j);
}
}
void hires(string n1,string n2){//n1雇佣n2
Tman *s1=hash[n1];//找到n1的地址
Tman *s2 = new Tman();
s2->name = n2;
s2->f = s1;
s1->s.push_back(s2);
hash[n2] = s2;
}//雇佣
void fires(string n1){
Tman *s1 = hash[n1];//找到n1的位置
Tman *fa = s1->f;
hash.erase(n1);
while(s1->s.size()!=0){
s1->name = s1->s.front()->name;//把它的名字等于下面的第一个儿子
hash[s1->name] = s1;//把这个值边变为第一个
s1 = s1->s.front();
}
s1->f->s.remove(s1);
delete s1;
}//解雇
int main(){
string s1,s2,a;
int i;
cin>>s1;
root = new Tman();
hash[s1] = root;
root->name = s1;
while(cin>>s1){
if(s1=="print"){
output(0,root);
for(i=0;i<60;i++)
cout<<'-';
cout<<endl;
}
else if(s1=="fire"){
cin>>s2;
fires(s2);
}
else{
cin>>a>>s2;
hires(s1,s2);
}
}
return 0;
}
POJ-->2003Hire and Fire
方法:就是通过C++的模版进行的映射。