HDU 5818 Joint Stacks 栈模拟

本文介绍了一种数据结构——可合并栈(mergeable stack),并提供了详细的实现方案。该栈支持三种基本操作:push、pop 和 merge,并通过样例解释了操作流程。文章还提供了一种高效实现方式,确保整体时间复杂度为 O(N)。

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

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

Joint Stacks

比较简单巧妙的一个做法是引入一个新的栈C,每次合并的时候就把A和B合并到C上,然后把A和B都清空. push还是按正常做,pop注意当遇到要pop的栈为空时,因为题目保证不会对空栈进行pop操作,所以这时应直接改为对C栈进行pop操作. 这样做因为保证每个元素最多只在一次合并中被处理到,pop和push操作当然也是每个元素只做一次,所以总复杂度是O(N)的. 另一种做法是用链表来直接模拟,复杂度也是O(N),但代码量稍大一些.

A, B入栈的时候加个时间戳, 方便合并。

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
const int MAXN = 100000 + 7;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    int cas = 1;
    while (cin >> n) {
        if (!n) break;
        cout << "Case #" << cas++ << ":" << endl;
        stack<pair<int, int> > a;
        stack<pair<int, int> > b;
        stack<int> c;
        string op;
        char cc;
        int num;
        int t = 0;
        for (int i = 0; i < n; ++i) {
            cin >> op;
            if (op == "push") {
                cin >> cc >> num;
                if (cc == 'A') {
                    a.push(make_pair(num, t++));
                } else {
                    b.push(make_pair(num, t++));
                }
            } else if (op == "pop") {
                cin >> cc;
                if (cc == 'A') {
                    if (!a.empty()) {
                        cout << a.top().first << endl;
                        a.pop();
                    } else {
                        cout << c.top() << endl;
                        c.pop();
                    }
                } else {
                    if (!b.empty()) {
                        cout << b.top().first << endl;
                        b.pop();
                    } else {
                        cout << c.top() << endl;
                        c.pop();
                    }
                }
            } else {
                char s1, s2;
                cin >> s1 >> s2;
                if (s1 == s2) continue;
                else {
                    stack<int> tmp;
                    while (!a.empty() || !b.empty()) {
                        if (a.empty()) {
                            while (!b.empty()) {
                                tmp.push(b.top().first);
                                b.pop();
                            }
                        }
                        if (b.empty()) {
                            while (!a.empty()) {
                                tmp.push(a.top().first);
                                a.pop();
                            }
                        }
                        if(!a.empty() && !b.empty()) {
                            if (a.top().second < b.top().second) {
                                tmp.push(b.top().first);
                                b.pop();
                            } else {
                                tmp.push(a.top().first);
                                a.pop();
                            }
                        }
                    }
                    while (!tmp.empty()) {
                        c.push((int &&) tmp.top());
                        tmp.pop();
                    }
                }
            }
        }
    }

return 0;
}


 
资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值