zoukankan      html  css  js  c++  java
  • 2016暑假多校联合---Joint Stacks (STL)

    HDU  5818

    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
     
    思路:用C栈保存A、B栈的合并,可以再用一个D栈保存A和B的合并,然后再导到C栈;
     
    代码如下:
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <stack>
    #include <queue>
    using namespace std;
    struct Node
    {
        int x;
        int t;
    };
    stack<Node*>A,B,C,D;
    
    int main()
    {
        int N,Case=1;
        char s[10];
        char x;
        int c;
        while(scanf("%d",&N)&&N)
        {
            printf("Case #%d:
    ",Case++);
            for(int i=1;i<=N;i++)
            {
                scanf("%s",s);
                if(s[1]=='u')
                {
                    scanf(" %c",&x);
                    scanf("%d",&c);
                    Node* a=new Node();
                    a->x=c;
                    a->t=i;
                    if(x=='A')
                        A.push(a);
                    else
                        B.push(a);
                }
                else if(s[1]=='o')
                {
                    scanf(" %c",&x);
                    if(x=='A')
                    {
                        if(A.empty())
                        {
                            printf("%d
    ",C.top()->x);
                            C.pop();
                        }
                        else
                        {
                            printf("%d
    ",A.top()->x);
                            A.pop();
                        }
                    }
                    else
                    {
                        if(B.empty())
                        {
                            printf("%d
    ",C.top()->x);
                            C.pop();
                        }
                        else
                        {
                             printf("%d
    ",B.top()->x);
                             B.pop();
                        }
                    }
                }
                else if(s[1]=='e')
                {
                    scanf(" %c",&x);
                    scanf(" %c",&x);
                    while(!A.empty()&&!B.empty())
                    {
                        if(A.top()->t>B.top()->t)
                        {
                            D.push(A.top());
                            A.pop();
                        }
                        else
                        {
                            D.push(B.top());
                            B.pop();
                        }
                    }
                    while(!A.empty())
                    {
                        D.push(A.top());
                            A.pop();
                    }
                    while(!B.empty())
                    {
                        D.push(B.top());
                            B.pop();
                    }
                    while(!D.empty())
                    {
                        C.push(D.top());
                        D.pop();
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    【Unity Addressables】Addressables源码移植优化(一)
    Unity Addressable热更系统尝试(一)
    python自动化测试,下载文件然后进行查询判断并且删除(比较准确!可用于多个重复的文件)
    python自动化测试-切换至iframe的具体操作。
    python自动化测试-给标签的属性值进行赋值。
    如何区分前后端bug
    python字符串中获取数字
    安装Xshell遇到的问题: xshell启动遇到由于找不到mfc110.dll,无法继续执行代码的解决方法/产品运行所需的信息检索失败。请重新安装xshell
    python自动化测试,遇到selenium.common.exceptions.ElementClickInterceptedException: Message: Element错的解决方法
    在线正则表达式校验
  • 原文地址:https://www.cnblogs.com/chen9510/p/5754627.html
Copyright © 2011-2022 走看看