省赛训练之C++的模版映射(六)

#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++的模版进行的映射。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值