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

题目

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
//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 A, B;
list *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;
}

你可能感兴趣的:(hdu,模拟,双向链表)