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

来源:互联网 发布:国外windows优化软件 编辑:程序博客网 时间:2024/04/28 07:05

题目

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 qiangusing 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;}
0 0
原创粉丝点击