poj2003 多链表节点删除,添加

1 篇文章 0 订阅
1 篇文章 0 订阅

                                                                                                          Hire and Fire
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1958 Accepted: 549

Description

In this problem, you are asked to keep track of the hierarchical structure of an organization's changing staff. As the first event in the life of an organization, the Chief Executive Officer (CEO) is named. Subsequently, any number of hires and fires can occur. Any member of the organization (including the CEO) can hire any number of direct subordinates, and any member of the organization (including the CEO) can be fired. The organization's hierarchical structure can be represented by a tree. Consider the example shown by Figure 1:

VonNeumann is the CEO of this organization. VonNeumann has two direct subordinates: Tanenbaum and Dijkstra. Members of the organization who are direct subordinates of the same member are ranked by their respective seniority. In the diagram, the seniority of such members decrease from left to right. For example Tanenbaum has higher seniority than Dijkstra.

When a member hires a new direct subordinate, the newly hired subordinate has lower seniority than any other direct subordinates of the same member. For example, if VonNeumann (in Figure 1) hires Shannon, then VonNeumann's direct subordinates are Tanenbaum, Dijkstra, and Shannon in order of decreasing seniority.

When a member of the organization gets fired, there are two possible scenarios. If the victim (the person who gets fired) had no subordinates, then he/she will be simply dropped from the organization's hierarchy. If the victim had any subordinates, then his/her highest ranking (by seniority) direct subordinate will be promoted to fill the resulting vacancy. The promoted person will also inherit the victim's seniority. Now, if the promoted person also had some subordinates then his/her highest ranking direct subordinate will similarly be promoted, and the promotions will cascade down the hierarchy until a person having no subordinates has been promoted. In Figure 1, if Tanenbaum gets fired, then Stallings will be promoted to Tanenbaum's position and seniority, and Knuth will be promoted to Stallings' previous position and seniority.

Figure 2 shows the hierarchy resulting from Figure 1 after (1) VonNeumann hires Shannon and (2) Tanenbaum gets fired:

Input

The first line of the input contains only the name of the person who is initially the CEO. All names in the input file consist of 2 to 20 characters, which may be upper or lower case letters, apostrophes, and hyphens. (In particular, no blank spaces.) Each name contains at least one upper case and at least one lower case letter.


The first line will be followed by one or more additional lines. The format of each of these lines will be determined by one of the following three rules of syntax:
  • [existing member] hires [new member]
  • fire [existing member]
  • print

Here [existing member] is the name of any individual who is already a member of the organization, [new member] is the name of an individual who is not a member of the organization as yet. The three types of lines (hires, fire, and print) can appear in any order, any number of times.

You may assume that at any time there is at least one member (who is the CEO) and no more than 1000 members in the organization.

Output

For each print command, print the current hierarchy of the organization, assuming all hires and fires since the beginning of the input have been processed as explained above. Tree diagrams (such as those in Figures 1 and 2) are translated into textual format according to the following rules:
  • Each line in the textual representation of the tree will contain exactly one name.
  • The first line will contain the CEO's name, starting in column 1.
  • The entire tree, or any sub-tree, having the form

    will be represented in textual form as:

The output resulting from each print command in the input will be terminated by one line consisting of exactly 60 hyphens. There will not be any blank lines in the output.

Sample Input

VonNeumann
VonNeumann hires Tanenbaum
VonNeumann hires Dijkstra
Tanenbaum hires Stallings
Tanenbaum hires Silberschatz
Stallings hires Knuth
Stallings hires Hamming
Stallings hires Huffman
print
VonNeumann hires Shannon
fire Tanenbaum
print
fire Silberschatz
fire VonNeumann
print

Sample Output

VonNeumann
+Tanenbaum
++Stallings
+++Knuth
+++Hamming
+++Huffman
++Silberschatz
+Dijkstra
------------------------------------------------------------
VonNeumann
+Stallings
++Knuth
+++Hamming
+++Huffman
++Silberschatz
+Dijkstra
+Shannon
------------------------------------------------------------
Stallings
+Knuth
++Hamming
+++Huffman
+Dijkstra
+Shannon
------------------------------------------------------------

没涉及到什么算法,说到底还是考基本功。练习多链表的好题,在这种题上多花点时间还是值得的

 

#include<iostream>
#include<list>
#include<map>
using namespace std;
struct human{
	string name;
	human* father;
	list<human*> son;
	human(){
		father=NULL;
	}
}; 
map<string,human *> hash;
void output(human* man,int ceng){
	if(man==NULL)return;
	for(int i=1;i<=ceng;i++)cout<<'+';
	cout<<man->name<<endl;
	for(list<human*>::iterator i=man->son.begin();i!=man->son.end();i++)
	  output(*i,ceng+1);
}
void hire(string boss,string worker){
	human* w=new human();
	w->name=worker;
	w->father=hash[boss];
	hash[boss]->son.push_back(w);
	hash[worker]=w;
}
void fire(string worker){
	human* f=hash[worker]->father;
	human* man=hash[worker];
	hash.erase(worker);
	while(man->son.size()!=0){
		man->name=man->son.front()->name;
		hash[man->name]=man;
		man=man->son.front();
	}
	man->father->son.remove(man);
	delete man;
}
int main(){
	string ceo,s,w;
	cin>>ceo;
	human* root=new human();
	root->name=ceo;
	hash[ceo]=root;
	while(cin>>s){
		if(s=="print"){
			output(root,0);
			for(int i=1;i<=60;i++)cout<<'-';
	            cout<<endl;
		}
		else if(s=="fire"){
			cin>>s;				
            fire(s);	
		}
		else{
			cin>>w;
			cin>>w;
			hire(s,w);
		}	
	}
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值