#include<iostream>
#include<list>
#include<vector>
#include<map>
#include<cstring>
#include<cstdio>
#include<string>
using namespace std;
struct stu
{
string name;
stu *parent;
list<stu *> son;
stu(){parent=NULL;}
};
map<string,stu*> hassh;
stu *root;
void hires(string n1,string n2)
{
stu *neww=new stu();
neww->name=n2;
neww->parent=hassh[n1];
hassh[n1]->son.push_back(neww);
hassh[n2]=neww;
}
void fire(string n)
{
stu *now=hassh[n];
stu *parent=now->parent;
hassh.erase(n);
while(now->son.size())//实在是太巧妙了,哈哈哈哈哈,今天最大的收获,换形不换神
{
now->name=now->son.front()->name;
hassh[now->name]=now;
now=now->son.front();
}
now->parent->son.remove(now);
delete now;
}
void print(int deep,stu *now)
{
if(now==NULL)
return;
for(int i=0;i<deep;i++)
putchar('+');
cout<<now->name<<endl;
for(list<stu*>::iterator it=now->son.begin();it!=now->son.end();++it)
print(deep+1,*it);
}
int main()
{
string s1,s2;
cin>>s1;
root=new stu();
hassh[s1]=root;
root->name=s1;
while(cin>>s1)
{
if(s1=="fire")
{
cin>>s2;
fire(s2);
}
else if(s1=="print")
{
print(0,root);
for(int i=0;i<=59;i++)
putchar('-');
putchar('\n');
}
else
{
cin>>s2;
cin>>s2;
hires(s1,s2);
}
}
return 0;
}
poj 2003 数据结构+先序遍历
最新推荐文章于 2019-08-06 21:13:20 发布