(使用数的便利求解层次性问题8.1.2)POJ 2003 Hire and Fire(元素的插入与删除)

/*
 * POJ_2003.cpp
 *
 *  Created on: 2013年11月5日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <map>
#include <list>
#include <string>

using namespace std;

struct Tman{
	string name;
	Tman* f;
	list<Tman*> s;
	Tman(){
		f = NULL;
	}
};

map<string,Tman*> hash;
Tman* root;

void print(long dep,Tman* now){
	if(now == NULL){
		return ;
	}

	long i;
	for(i = 1 ; i <= dep ; ++i){
		cout<<'+';
	}
	cout<<now->name<<endl;
	for(list<Tman*>::iterator it = now->s.begin(); it != now->s.end() ; ++it){
		print(dep+1,*it);
	}
}

void hire(string n1,string n2){
	Tman* f = hash[n1];
	Tman* s = new Tman();

	s->name = n2;
	s->f = f;
	f->s.push_back(s);
	hash[n2] = s;
}

void fire(string n1){
	Tman* s = hash[n1];
	Tman* f = s->f;
	hash.erase(n1);
	while(s->s.size() != 0){
		s->name = s->s.front()->name;
		hash[s->name] = s;
		s = s->s.front();
	}
	s->f->s.remove(s);
	delete s;
}

void solve(){
	string s1,s2;
	long i;
	cin >> s1;
	root = new Tman();
	hash[s1] = root;
	root->name = s1;
	while(cin >> s1){
		if(s1 == "print"){
			print(0,root);
			for(i = 1 ; i <= 60 ; ++i){
				cout<<'-';
			}
			cout<<endl;
		}else if(s1 == "fire"){
			cin >> s2;
			fire(s2);
		}else{
			cin >> s2;
			cin >> s2;
			hire(s1,s2);
		}
	}
}

int main(){
	solve();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值