杭电OJ 5818 Joint Stacks

Problem Description
A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is the first one that will be removed. In another word, the operations perform in a Last-In-First-Out (LIFO) manner.
A mergeable stack is a stack with "merge" operation. There are three kinds of operation as follows:

- push A x: insert x into stack A
- pop A: remove the top element of stack A
- merge A B: merge stack A and B

After an operation "merge A B", stack A will obtain all elements that A and B contained before, and B will become empty. The elements in the new stack are rearranged according to the time when they were pushed, just like repeating their "push" operations in one stack. See the sample input/output for further explanation.
Given two mergeable stacks A and B, implement operations mentioned above.
 

Input
There are multiple test cases. For each case, the first line contains an integer N(0<N105) , indicating the number of operations. The next N lines, each contain an instruction "push", "pop" or "merge". The elements of stacks are 32-bit integers. Both A and B are empty initially, and it is guaranteed that "pop" operation would not be performed to an empty stack. N = 0 indicates the end of input.
 

Output
For each case, print a line "Case #t:", where t is the case number (starting from 1). For each "pop" operation, output the element that is popped, in a single line.
 

Sample Input
  
  
4 push A 1 push A 2 pop A pop A 9 push A 0 push A 1 push B 3 pop A push A 2 merge A B pop A pop A pop A 9 push A 0 push A 1 push B 3 pop A push A 2 merge B A pop B pop B pop B 0
 

Sample Output
  
  
Case #1: 2 1 Case #2: 1 2 3 0 Case #3: 1 2 3 0
 

Author
SYSU
 

Source
 

Recommend
wange2014
 

读完题意 可以知道  这道题 跟栈有关系,可以知道题上涉及两个栈,但是我们可以定义四个栈,a,b,可以在没有出现merge的时候,存下a,b里面的数据 而且方便出栈,另外,再有两个栈,d是负责中间(相当于中间变量),方便给c栈,这样可以出来的时候符合题意。

另外注意:栈是一头进一头出(同一头)。


#include<algorithm>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stack>
using namespace std;
struct node
{
    int id;
    int shu;
};
int main()
{
    int n,t1=1;
    while(~scanf("%d",&n)&&n)
    {
        stack<node>a;
        stack<node>b;
        stack<node>c;
        stack<node>d;
        node num;
        char str[10],s[10],s1[10];
        int t=0,i;
        printf("Case #%d:\n",t1++);
        while(n--)
        {
            scanf("%s",str);
            if(strcmp(str,"push")==0)
            {
                scanf("%s%d",s,&num.shu);
                if(s[0]=='A')
                {
                    num.id=t;
                    a.push(num);
                    t++;
                }
                else
                {
                    num.id=t;
                    b.push(num);
                    t++;
                }
            }
            else if(strcmp(str,"pop")==0)
            {
                scanf("%s",s);
                if(s[0]=='A')
                {
                    if(!a.empty())
                    {
                        printf("%d\n",a.top().shu);
                        a.pop();
                    }
                    else
                    {
                        printf("%d\n",c.top().shu);
                        c.pop();
                    }
                }
                if(s[0]=='B')
                {
                    if(!b.empty())
                    {
                        printf("%d\n",b.top().shu);
                        b.pop();
                    }
                    else
                    {
                        printf("%d\n",c.top().shu);
                        c.pop();
                    }
                }
            }
            else if(strcmp(str,"merge")==0)
            {
                scanf("%s%s",s,s1);
                while(!a.empty()&&(!b.empty()))
                {
                    node numa=a.top(),numb=b.top();
                    if(numa.id>numb.id)
                    {
                        d.push(numa);
                        a.pop();
                    }
                    else
                    {
                        d.push(numb);
                        b.pop();
                    }
                }
                while(!a.empty())
                {
                    d.push(a.top());
                    a.pop();
                }
                while(!b.empty())
                {
                    d.push(b.top());
                    b.pop();
                }
                while(!d.empty())
                {
                    c.push(d.top());
                    d.pop();
                }
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

细水长流者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值