zoukankan      html  css  js  c++  java
  • HDU 5818 Joint Stacks

    Joint Stacks

    Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 610    Accepted Submission(s): 282


    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
     
    list模拟....其实和栈的作用差不多,本来想用list中的merge的方法的,但是太慢了超时.....
    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/8/9 12:30:47
    File Name     :p710.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 100010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    
    struct node{
        ll x;
        int id;
    };
    bool cmp(node a,node b){
        return a.id<b.id;
    }
    list<node> a,c,b,d;
    ll num;
    char s[10],ch[3],w[3];
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int n;
        int cas=0;
        while(cin>>n&&n){
            printf("Case #%d:
    ",++cas);
            a.clear(),b.clear(),c.clear(),d.clear();
            int mark=0;
            for(int i=1;i<=n;i++){
                scanf("%s %s",&s,&ch);
                if(strcmp(s,"push")==0){
                    scanf("%I64d",&num);
                    if(ch[0]=='A')a.push_back({num,i});
                    else b.push_back({num,i});
                }
                else if(strcmp(s,"pop")==0){
                    if(ch[0]=='A'){
                        if(a.empty()){
                            printf("%I64d
    ",c.back().x);
                            c.pop_back();
                            continue;
                        }
                        printf("%I64d
    ",a.back().x);
                        a.pop_back();
                    }
                    else {
                        if(b.empty()){
                            printf("%I64d
    ",c.back().x);
                            c.pop_back();
                            continue;
                        }
                        printf("%I64d
    ",b.back().x);
                        b.pop_back();
                    }
                }
                else{
                    scanf("%s",&w);
                     while(!a.empty() && !b.empty()){
                        int aa=a.back().id;
                        int bb=b.back().id;
                        if (aa>bb){
                            d.push_back(a.back());
                            a.pop_back();
                        }
                        else{
                            d.push_back(b.back());
                            b.pop_back();
                        }
                    }
                    while(!a.empty()){
                        d.push_back(a.back());
                        a.pop_back();
                    }
                    while(!b.empty()){
                        d.push_back(b.back());
                        b.pop_back();
                    }
                    while(!d.empty()){
                        c.push_back(d.back());
                        d.pop_back();
                    }
                }
            }
        }
        return 0;
    }
     
     
  • 相关阅读:
    go语言判断末尾不同的长字符串的方法
    Go语言高级特性总结——Struct、Map与JSON之间的转化
    MacOS使用常用配置
    关于联盟链的一种激励扩张思路(原创)
    密码学中经典算法及应用
    无线网络
    基础的并查集
    找单词
    找零钱
    最大子矩阵
  • 原文地址:https://www.cnblogs.com/pk28/p/5755535.html
Copyright © 2011-2022 走看看