(使用数的便利求解层次性问题8.1.2)POJ 2003 Hire and Fire(元素的插入与删除)
2013-11-05 09:32
656 查看
/* * 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; }
相关文章推荐
- 博弈搜索练习--POJ-2003 Hire and Fire
- POJ 2003 Hire and Fire (Tree)
- POJ 2003 Hire and Fire (多重链表 树结构 好题)
- POJ---2003-Hire and Fire
- POJ 2003 Hire and Fire
- POJ 2003 Hire and Fire (多重链表 树结构 好题)
- STL中,deque插入或删除元素时,迭代器是否失效的问题
- poj-1322-Chocolate 使用动态规划求解的一种概率问题的算法
- (用树的遍历求解层次性问题8.1.1)POJ 1330 Nearest Common Ancestors(求解最近共同祖先)
- objecttive-C语言使用二叉排序树实现查找、插入、删除、查看元素
- (利用树的遍历求解层次性问题8.4.10)POJ 1634 Who's the boss?(求解某一个节点的父亲以及他的孩子的数目)
- 关于vector中使用erase删除元素后导致越界的问题
- Django 使用 MySQL 存储时间中遇到的问题(在数据库中记录插入时间、更新时间、删除时间)
- POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)
- A - Hire and Fire(8.1.2)
- 在编写动态插入删除DOM元素时,Easyui控件渲染效果失败的问题
- A - Hire and Fire(8.1.2)
- java中ArrayList使用remove删除元素时几种常见的问题及解决办法
- (并查集..含小总结)A - Hire and Fire(8.1.2)
- (Relax dijkstra1.2)POJ 2263 Heavy Cargo(使用dijkstra来求解最大生成树问题)