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

时间:2022-07-21 01:08:30


/*
 * 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;
}