(使用数的便利求解层次性问题8.1.2)POJ 2003 Hire a

当前位置 : 首页 > 网页制作 > CSS > (使用数的便利求解层次性问题8.1.2)POJ 2003 Hire a

(使用数的便利求解层次性问题8.1.2)POJ 2003 Hire a

来源: 作者: 时间:2016-01-28 09:27
* * POJ_2003 cpp * * Created on: 2013年11月5日 * Author: Administrator * include iostream include cstdio include map inc
 
/*  
 * 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;  
}  

 

Tag:
网友评论

<