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): 828    Accepted Submission(s): 403


    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
     
    Author
    SYSU
     
    Source
     
     
     
    解析:模拟。为了操作方便,引入栈C。每次合并的时候把A和B合并到C上,然后把A和B都清空。push操作不变,但进行pop操作时,如果pop的栈为空,改为对栈C进行操作。这样做的原因是题目保证不会对空栈进行pop操作,且pop操作进行后,pop出的元素不会再用到。
     
     
     
     
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    const int MAXN = 1e5+5;
    int x[MAXN];    //记录数值
    int s[3][MAXN]; //三个栈(s[0][]代表A, s[1][]代表B, s[2]代表C)记录时间戳,即x[]的下标
    int top[3];     //三个栈的栈顶
    int n;
    
    void solve()
    {
        char op[10], obj[5];
        top[0] = top[1] = top[2] = 0;
        for(int i = 0; i < n; ++i){
            scanf("%s%s", op, obj);
            int oo = obj[0]-'A';
            if(op[1] == 'u'){
                scanf("%d", &x[i]);
                s[oo][top[oo]++] = i;   //记录时间戳i,对应栈顶++
            }
            else if(op[1] == 'o'){
                if(top[oo] == 0)    //要pop的栈为空时,改为对C栈进行操作
                    oo = 2;
                printf("%d
    ", x[s[oo][--top[oo]]]);
            }
            else{
                scanf("%s", obj);
                //合并栈A和栈B放到栈C(合并的是时间戳)
                top[2] = merge(s[0], s[0]+top[0],
                               s[1], s[1]+top[1],
                               s[2]+top[2]) - s[2];
                top[0] = top[1] = 0;
            }
        }
    }
    
    int main()
    {
        int cn = 0;
        while(scanf("%d", &n), n){
            printf("Case #%d:
    ", ++cn);
            solve();
        }
        return 0;
    }
    

      

  • 相关阅读:
    第十二章 并发编程
    信息安全系统设计基础实验四:外设驱动程序设计 20145222黄亚奇 20145213祁玮
    Appium学习笔记||四、获取app页面元素
    安装python3.6(附带PyCharm安装)
    Appium学习笔记||三、获取apk的appPackage和appActivity
    Appium学习笔记||二、环境准备
    Appium学习笔记||一、介绍
    Selenium学习笔记||十九、改变窗口大小
    Selenium学习笔记||十八、使用浏览器copy定位元素表达式
    Selenium学习笔记||十七、制造断点,来查看隐藏菜单元素
  • 原文地址:https://www.cnblogs.com/inmoonlight/p/5756820.html
Copyright © 2011-2022 走看看