多校賽第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<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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章