多校赛第7场 hdu5818 Joint Stacks 双向链表

1 篇文章 0 订阅
1 篇文章 0 订阅

题目

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.

这个题有很多方法,不过双向链表复杂度比较低

题意

就是模拟栈的操作,但是有一个合并的操作。

思路

我用四个链表进行维护,A和B存的是指向大链表的指针,也就是说最初A和B就是已经合并的状态,这样免去的合并的时间。

代码

#include<bits/stdc++.h>
//orz zhen shi qiang
using namespace std;

typedef long long ll;

int N;
char s[10];
char s2[5];
char s3[5];

typedef list<int>::iterator lit; 

list<int> his, now;
list<lit> A, B;
list<lit> *a, *b;

int main() {
    int cs = 1;
    while(~scanf("%d",&N) && N) {
        printf("Case #%d:\n",cs++);
        A.clear();
        B.clear();
        his.clear();
        now.clear();
        a = &A, b = &B;
        while(N--) {
            scanf("%s%s",s,s2);
            if(s[1] == 'u') {
                int n;
                scanf("%d",&n);
                if(s2[0] == 'A') {
                    now.push_back(n);
                    lit t = now.end();
                    t--;
                    a->push_back(t);
                }
                else {
                    now.push_back(n);
                    lit t = now.end();
                    t--;
                    b->push_back(t);
                }
            }
            else if(s[1] == 'e') {
                scanf("%s",s3);
                his.insert(his.end(), now.begin(), now.end());
                now.clear();
                //if(s2[0] == 'B') swap(a, b);
                a->clear(); b->clear();
                //printf("??%d\n",a->empty());
            }
            else {
                //printf("s2[0]=%c\n",s2[0]);
                if(s2[0] == 'A') {
                    //printf("-------%d\n",a->empty());
                    if(A.empty()) {
                        printf("%d\n",his.back());
                        his.pop_back();
                    }
                    else {
                        printf("%d\n",*A.back());
                        now.erase(A.back());
                        A.pop_back();
                    }
                }
                else {
                    if(B.empty()) {
                        printf("%d\n",his.back());
                        his.pop_back();
                    }
                    else {
                        printf("%d\n",*B.back());
                        now.erase(B.back());
                        B.pop_back();
                    }
                }
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值