HDU5818 Joint Stacks

本文介绍了一种特殊的数据结构——可合并栈,并详细解释了如何通过编程实现其基本操作,包括push、pop及merge。在merge操作中,两个栈的内容会根据元素的推入顺序进行合并。文章提供了一个具体的实现示例,帮助读者理解这些操作的具体流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Joint Stacks

                                                                      Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                                 Total Submission(s): 1815    Accepted Submission(s): 815


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
 

—————————————————————————————————

题目的意思给出两个栈A B(初始时为空),有三种操作:
push、pop、merge.
其中merge是按照A B中元素进栈的相对顺序来重排的.


思路:因为题目说了不会对空栈弹出,所以开三个优队(栈也可以)前两个分别存A和B,第三个公用,插入删除还是对AB操作,合并把AB情况放到C中,每次输入时先判AB是否是空,不为空则输出AB顶,否则这个元素肯定在C中输出即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n,x;
char s1[10],s2[5],s3[5];
int cnt [1000005],fl[1000006];
struct node
{
    int t,val;
    friend bool operator <(const node &a,const node &b)
    {
        return a.t<b.t;
    }
} pre;

int main()
{
    int cas=0;
    while(~scanf("%d",&n)&&n)
    {
        printf("Case #%d:\n",++cas);
        priority_queue<node>q1,q2,q3;
        for(int i=1; i<=n; i++)
        {
            scanf("%s",s1);
            if(!strcmp(s1,"push"))
            {
                scanf("%s%d",s2,&x);
                pre.t=i,pre.val=x;
                if(!strcmp(s2,"A")) q1.push(pre);
                else q2.push(pre);
            }
            else if(!strcmp(s1,"pop"))
            {
                scanf("%s",s2);
                if(!strcmp(s2,"A"))
                {
                    if(!q1.empty())
                        printf("%d\n",q1.top().val),q1.pop();
                    else
                        printf("%d\n",q3.top().val),q3.pop();
                }
                else
                {
                    if(!q2.empty())
                    printf("%d\n",q2.top().val),q2.pop();
                     else
                        printf("%d\n",q3.top().val),q3.pop();
                }
            }
            else
            {
                scanf("%s%s",s2,s3);
                    while(!q2.empty())
                        q3.push(q2.top()),q2.pop();
                    while(!q1.empty())
                        q3.push(q1.top()),q1.pop();
            }
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值