zoukankan      html  css  js  c++  java
  • Joint Stacks---hdu5818(栈模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5818

     有3个操作pop,push,merge A B;

    引入一个新的栈C,每次合并的时候就把A和B合并到C上,然后把A和B都清空. push还是按正常做,

    pop时注意当遇到要pop的栈为空时,因为题目保证不会对空栈进行pop操作,所以这时应直接改为对C栈进行pop操作.

    这样做因为保证每个元素最多只在一次合并中被处理到,pop和push操作当然也是每个元素只做一次,所以总复杂度是O(N)的;

     

    #include <cstring>
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <stack>
    #include <vector>
    #include <queue>
    using namespace std;
    #define N 105
    #define met(a, b) memset(a, b, sizeof(a))
    
    typedef long long LL;
    
    struct node
    {
        int Id, num;
        node(){}
        node(int Id, int num) : Id(Id), num(num){}
    };
    
    int main()
    {
        int n, tCase = 1;
        while(scanf("%d", &n) , n)
        {
            stack<node> a;
            stack<node> b;
            stack<node> c;
            stack<node> t;
            printf("Case #%d:
    ", tCase++);
            for(int i=1; i<=n; i++)
            {
                char s1[15], s2[15];
                int num;
                scanf("%s", s1);
                if(strcmp(s1, "push") == 0)
                {
                    scanf("%s %d", s2, &num);
                    if(s2[0] == 'A')
                        a.push(node(i, num));
                    else
                        b.push(node(i, num));
                }
                else if(strcmp(s1, "pop") == 0)
                {
                    scanf("%s", s2);
                    if(s2[0] == 'A')
                    {
                        if(!a.empty())
                        {
                            node p = a.top();a.pop();
                            printf("%d
    ", p.num);
                        }
                        else
                        {
                            node p = c.top();c.pop();
                            printf("%d
    ", p.num);
                        }
                    }
                    else
                    {
                        if(!b.empty())
                        {
                            node p = b.top();b.pop();
                            printf("%d
    ", p.num);
                        }
                        else
                        {
                            node p = c.top();c.pop();
                            printf("%d
    ", p.num);
                        }
                    }
                }
                else
                {
                    node p, q;
                    scanf("%s %s", s1, s2);
                    while(!a.empty() && !b.empty())
                    {
                        p = a.top();
                        q = b.top();
                        if(p.Id > q.Id)
                        {
                            t.push(p);
                            a.pop();
                        }
                        else
                        {
                            t.push(q);
                            b.pop();
                        }
                    }
                    while(!a.empty())
                    {
                        p = a.top();
                        a.pop();
                        t.push(p);
                    }
                    while(!b.empty())
                    {
                        p = b.top();
                        b.pop();
                        t.push(p);
                    }
    
                    while(!t.empty())
                    {
                        p = t.top();
                        t.pop();
                        c.push(p);
                    }
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Xcode8 去除系统日志输出
    SVN参考命令
    Xcode模拟网络状态
    iOS 图片拉伸
    iOS进阶
    label中添加图片
    Cookie的格式及组成
    java数据类型总结
    Hibernate一级缓存与二级缓存的区别
    mysql连接jdbc查询代码
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5755700.html
Copyright © 2011-2022 走看看