That thing in the company

describe

I'm afraid the first thing a company does when it's set up is appoint a CEO.After that, the CEO will start hiring employees, as well as leaving other companies.Assuming that every employee (including the CEO) in a company can directly hire new employees, and that all employees (including the CEO) in the company may also leave, the organization structure of a company after its establishment is shown in Figure 1:

Von Neumann, the company's CEO, hired two people directly, Tanenbaum and Dijkstra.In a company, there are several direct subordinates of an employee whose positions are determined by their length of service, which is shown in the figure as lower and lower positions from left to right. Tanenbaum, for example, has a higher position than Dijkstra.

When an employee hires a new subordinate, the position of the subordinate is the lowest among all the subordinates hired by the employee.Assuming Von Neumann hires Shannon again, the three subordinate positions of Von Neumann range from high to low, respectively, Tanenbaum, Dijkstra and Shannon.

When an employee leaves the company, there are two situations.If he does not hire any subordinates, he will be removed from the organization structure of the company.If he has a direct subordinate, the higher-ranking person in his direct subordinate will be promoted and filled in.If the subordinate also has subordinates, the highest position among his subordinates will make up for the vacancy he has made after his promotion.By analogy, until an employee who has not hired a subordinate is promoted.

Assuming the Tanenbaum chute in Figure 1 leaves, Stallings will fill its position and Knuth will fill its position.Figure 2 shows the result of the change: (1) VonNeumann hired Shannon, (2) Tanenbaum job-hopping.

input

The first line you enter is the name of the CEO.All names in the title are between 2 and 20 in length and consist of upper and lower case letters, numbers, and dashes (minus signs).Each name contains at least one upper case and one lower case letter.

After the first line there are many lines of content, which consist of the following rules:

  • [Old Employee] hires [New Employee]
  • fire [old employee]
  • print

The [old employee] is the name of the person who has already worked in the company and the [new employee] is the name of the employee who will be hired.The content of the above three rules may appear in any order.But there will be at least one employee (CEO) in the company, and the company will be no larger than 1,000.

output

For each print command, output the structure information of the current company according to the following rules:

Each line contains a name

The first line is the name of the CEO, starting with the first column

The result in Figure 3,

Will output as Figure 4

After each print, a minus sign of 60 characters is output, with no blank lines in the whole output.

  Test Input Expected Output Time limit Memory Limit Extra Process
Test Case 1 Display as text
  1. VonNeumann↵
  2. VonNeumann hires Tanenbaum↵
  3. VonNeumann hires Dijkstra↵
  4. Tanenbaum hires Stallings↵
  5. Tanenbaum hires Silberschatz↵
  6. Stallings hires Knuth↵
  7. Stallings hires Hamming↵
  8. Stallings hires Huffman↵
  9. print↵
  10. VonNeumann hires Shannon↵
  11. fire Tanenbaum↵
  12. print↵
  13. fire Silberschatz↵
  14. fire VonNeumann↵
  15. print↵
Display as text
  1. VonNeumann↵
  2. +Tanenbaum↵
  3. ++Stallings↵
  4. +++Knuth↵
  5. +++Hamming↵
  6. +++Huffman↵
  7. ++Silberschatz↵
  8. +Dijkstra↵
  9. ------------------------------------------------------------↵
  10. VonNeumann↵
  11. +Stallings↵
  12. ++Knuth↵
  13. +++Hamming↵
  14. +++Huffman↵
  15. ++Silberschatz↵
  16. +Dijkstra↵
  17. +Shannon↵
  18. ------------------------------------------------------------↵
  19. Stallings↵
  20. +Knuth↵
  21. ++Hamming↵
  22. +++Huffman↵
  23. +Dijkstra↵
  24. +Shannon↵
  25. ------------------------------------------------------------↵
1 second 64M 0
#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
  
typedef struct node{  
    char name[25];  
    struct node *successor;  //Successor   
    struct node* peer;  
}human;  
  
human *top,*root,*poorguy,*above;  
char er[25],ee[25];  
  
void creathuman(human **p)  //References must be used here  
{  
    *p=(human*)malloc(sizeof(human));  
    memset((*p)->name,'\0',25);  
    strcpy((*p)->name,ee);  
    (*p)->successor=(*p)->peer=NULL;  
    return;  
}  
  
int hire(human *p)  
{  
    if(p!=NULL)   
    {  
        if(!strcmp(p->name,er))  
        {  
            if(p->successor==NULL)  
            {  
                creathuman(&p->successor);  
                return 1;  
            }  
            else  
            {  
                p=p->successor;  
                while(p->peer!=NULL) p=p->peer; //Find Insertion Point   
                creathuman(&p->peer);  
                return 1;  
            }  
        }  
        else  
        {  
            if(hire(p->peer)) return 1;  
            if(hire(p->successor)) return 1;   
            return 0;  
        }  
    }  
    else return 0;  
      
}  
  
void print(human *p,int dep)  
{  
    if(p==NULL) return;  
    else  
    {  
        for(int i=0;i<dep;++i) printf("+");  
        printf("%s\n",p->name);  
        print(p->successor,dep+1);  
        print(p->peer,dep);  
    }     
}  
  
int find(human *pp)  //Find out if any of his subordinates are expelled   
{  
    human *p=pp->successor;  
    while(p!=NULL)  
    {  
        if(!strcmp(p->name,ee))  
        {  
            above=pp;  
            poorguy=p;  
            return 1;  
        }  
        else p=p->peer;  
    }  
    p=pp->successor;//No, find subordinates   
    while(p!=NULL)  
    {  
        if(find(p)) return 1;  
        p=p->peer;   
    }  
    return 0;     
}  
  
void arrange(human *p)  
{  
    if(p->successor->successor==NULL)  
    {  
        memset(p->name,'\0',25);  
        strcpy(p->name,p->successor->name);  
        p->successor=p->successor->peer;  
        return;  
    }  
    memset(p->name,'\0',25);  
    strcpy(p->name,p->successor->name);  
    arrange(p->successor);  
}  
  
void fire()  
{  
    if(poorguy->successor==NULL) //Expulsed person has no subordinates   
    {  
        if(above->successor==poorguy)  
        {  
            above->successor=above->successor->peer;  
            return;  
        }  
        else  
        {  
            human *p=above->successor;  
            while(p->peer!=poorguy) p=p->peer;  
            p->peer=p->peer->peer;   
        }  
    }  
    else  
    {  
         arrange(poorguy);   
    }  
}  
  
  
int main(){  
    //Boss   
    char name[25],operate[25];  
    memset(operate,'\0',25);  
    memset(name,'\0',25);  
    scanf("%s",name);  
    top=(human*)malloc(sizeof(human));  
    memset(top->name,'\0',25); //Initialization   
    strcpy(top->name,name);  
    top->successor=top->peer=NULL;  
    root = (human*)malloc(sizeof(human));  
    root->successor=top;  
    root->peer=NULL;  
    //operation  
      
    while(scanf("%s",operate)!=EOF)  
    {  
        if(!strcmp(operate,"print"))  
        {  
            print(top,0);  
            for(int i=0;i<60;++i) printf("-");  
            printf("\n");  
        }  
        else if(!strcmp(operate,"fire"))  
        {  
            memset(er,'\0',25);  
            memset(ee,'\0',25);  
            scanf("%s",ee);  
            find(root);  
            fire(); //Expel  
        }  
        else //hire   
        {  
            memset(er,'\0',25);  
            memset(ee,'\0',25);  
            strcpy(er,operate);  
            scanf("%s",operate);  
            scanf("%s",ee);  
            hire(top);  
        }  
    }   
       
}  

 

61 original articles published, 9 praised, 2098 visits
Private letter follow

Posted on Mon, 16 Mar 2020 20:37:19 -0700 by ilikemath2002