#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char name[25];
int data;
struct node *next;
};
void pd(struct node *&p,int bj)
{
while(p && p->data == bj)
{
printf(" %s",p->name);
p = p->next;
}
}
void A(struct node *head)
{
struct node *p = head->next,*q = head,*r;
r = (struct node *)malloc(sizeof(struct node));
scanf("%s%d",r->name,&r->data);
while(p)
{
if(r->data > p->data)
{
q->next = r;
r->next = p;
break;
}
q = p;
p = p->next;
}
if(!p)
{
q->next = r;
r->next = NULL;
}
}
void Q(struct node *head)
{
char sr[25];
struct node *p = head->next,*q = head;
scanf("%s",sr);
while(p)
{
if(!strcmp(p->name,sr))
{
q->next = p->next;
free(p);
break;
}
q = p;
p = p->next;
}
}
void C(struct node *head)
{
int add;
char nm[25];
scanf("%s%d",nm,&add);
struct node *p = head->next,*q = head;
while(p)
{
if(!strcmp(nm,p->name))
{
q->next = p->next;
break;
}
q = p;
p=p->next;
}
p->data += add;
q = head;
while(q->next)
{
if(p->data > q->next->data)
{
p->next = q->next;
q->next = p;
break;
}
q = q->next;
}
if(!q->next)
{
q->next = p;
p->next = NULL;
}
}
void S(struct node *head)
{
struct node *p = head->next;
while(p)
{
printf("%s %d\n",p->name,p->data);
p = p->next;
}
}
void O(struct node *head)
{
int bj;
struct node *p = head->next;
printf("#1 :");
printf(" %s",p->name);
bj = p->data;
p = p->next;
pd(p,bj);
printf("\n");
printf("#2 :");
for(int i = 1; p && i <= 2; i++)
{
printf(" %s",p->name);
bj = p->data;
p = p->next;
}
pd(p,bj);
printf("\n");
printf("#3 :");
for(int i = 1; p && i <= 3; i++)
{
printf(" %s",p->name);
bj = p->data;
p = p->next;
}
pd(p,bj);
printf("\n");
}
int main()
{
int n;
char sr[10];
struct node *head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
scanf("%d",&n);
for(int i = 0; i < n; i++)
A(head);
while(scanf("%s",sr))
{
if(sr[0] == 'A')
{
A(head);
}
else if(sr[0] == 'Q')
{
Q(head);
}
else if(sr[0] == 'C')
{
C(head);
}
else if(sr[0] == 'S')
{
S(head);
}
else if(sr[0] == 'O')
{
// printf("\n");
O(head);
break;
}
}
return 0;
}
/***************************************************
User name: TJRAC6015203228魏杰
Result: Accepted
Take time: 12ms
Take Memory: 152KB
Submit time: 2016-11-02 13:45:51
****************************************************/