zoukankan      html  css  js  c++  java
  • [STL][list]Mergeable Stack

    Mergeable Stack

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    Given initially empty stacks, there are three types of operations:

    • 1 s v: Push the value onto the top of the -th stack.

    • 2 s: Pop the topmost value out of the -th stack, and print that value. If the -th stack is empty, pop nothing and print "EMPTY" (without quotes) instead.

    • 3 s t: Move every element in the -th stack onto the top of the -th stack in order.

      Precisely speaking, denote the original size of the -th stack by , and the original size of the -th stack by . Denote the original elements in the -th stack from bottom to top by , and the original elements in the -th stack from bottom to top by .

      After this operation, the -th stack is emptied, and the elements in the -th stack from bottom to top becomes . Of course, if , this operation actually does nothing.

    There are operations in total. Please finish these operations in the input order and print the answer for every operation of the second type.

    Input

    There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

    The first line contains two integers and ( ), indicating the number of stacks and the number of operations.

    The first integer of the following lines will be ( ), indicating the type of operation.

    • If , two integers and ( , ) follow, indicating an operation of the first type.
    • If , one integer ( ) follows, indicating an operation of the second type.
    • If , two integers and ( , ) follow, indicating an operation of the third type.

    It's guaranteed that neither the sum of nor the sum of over all test cases will exceed .

    Output

    For each operation of the second type output one line, indicating the answer.

    Sample Input

    2
    2 15
    1 1 10
    1 1 11
    1 2 12
    1 2 13
    3 1 2
    1 2 14
    2 1
    2 1
    2 1
    2 1
    2 1
    3 2 1
    2 2
    2 2
    2 2
    3 7
    3 1 2
    3 1 3
    3 2 1
    2 1
    2 2
    2 3
    2 3
    

    Sample Output

    13
    12
    11
    10
    EMPTY
    14
    EMPTY
    EMPTY
    EMPTY
    EMPTY
    EMPTY
    EMPTY

    思路:用链表结构来模拟栈的增加、删除元素,合并栈的操作。可以手写链表结构或直接使用STL里的list

    下面总结一些常用的list的用法:
    //定义list容器a
    list<int > a;//定义一个元素为int型的list
    list<int > a(n);//定义一个有n个元素且元素为int型的list,每个元素都是0
    list<int > a(n,m);//同上,不过每个元素都是m
    
    //基本操作
    a.clear();//清空list中的所有元素
    a.empty();//判断list是否为空
    a.front();//获得list的头部元素
    a.back();//获得list的最后一个元素
    a.push_front();//从list的头部插入元素
    a.push_back();//从list的末端插入元素
    a.pop_front();//删掉头部第一个元素
    a.pop_back();//删掉尾部第一个元素
    a.splice(a.begin(),b);//将链表b插入到链表a的链头
    a.splice(a.end(),b);//将链表b插入到链表a的链尾


    AC代码:
    #include <iostream>
    #include<cstdio>
    #include<list>
    using namespace std;
    
    list<int> a[300010];
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--){
            int n,q;
            scanf("%d%d",&n,&q);
            for(int i=0;i<300010;i++) a[i].clear();
            while(q--){
                int op;
                scanf("%d",&op);
                if(op==1){
                    int stk,x;
                    scanf("%d%d",&stk,&x);
                    a[stk].push_back(x);
                }
                if(op==2){
                    int stk;
                    scanf("%d",&stk);
                    if(!a[stk].empty()) {
                        printf("%d
    ",a[stk].back());
                        a[stk].pop_back();
                    }
                    else printf("EMPTY
    ");
                }
                if(op==3){
                    int stk1,stk2;
                    scanf("%d%d",&stk1,&stk2);
                    a[stk1].splice(a[stk1].end(),a[stk2]);
                }
            }
        }
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    一个printf(结构体指针)引发的血案
    C语言指针从底层原理到花式技巧,用图文和代码帮你讲解透彻
    利用C语言中的setjmp和longjmp,来实现异常捕获和协程
    提高代码逼格的利器:宏定义从入门到放弃
    我最喜欢的进程之间通信方式消息总线
    推荐一个阅读代码、文档的利器:屏幕贴图工具
    计算机网络基础知识总结
    vi指令总结
    LVS 负载均衡器理论基础及抓包分析
    Linux命令总结大全,包含所有linux命令
  • 原文地址:https://www.cnblogs.com/lllxq/p/8737231.html
Copyright © 2011-2022 走看看