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)


    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
     
    题意:

    现在有两个栈,有入栈和出栈和合并的操作,问每次出栈的数字是多少;
     
    思路:
     
    开三个栈,模拟这几种操作,当出栈时发现当前栈为空时就跳到第三个栈,
    想用链表模拟可是感觉不好写;
     
    AC代码:
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <bits/stdc++.h>
    #include <stack>
    #include <map>
     
    using namespace std;
     
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
     
    typedef  long long LL;
     
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
     
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=1e18;
    const int N=1e5+10;
    const int maxn=5e3+4;
    const double eps=1e-12;
    
    stack<int>a,b,c,d;
    
    char s[10],st[10],str[10];
    
    LL temp[N];
    int main()
    {
        int Case=0;
        LL x;
        while(1)
        {
            int n,cnt=0;
            read(n);
            if(n==0)break;
            while(!a.empty())a.pop();
            while(!b.empty())b.pop();
            while(!c.empty())c.pop();
            printf("Case #%d:
    ",++Case);
            For(i,1,n)
            {
                scanf("%s%s",s,str);
                if(s[0]=='p')
                {
                    if(s[1]=='u')
                    {
                        scanf("%lld",&x);
                        temp[++cnt]=x;
                        if(str[0]=='A')a.push(cnt);
                        else b.push(cnt);
                    }
                    else 
                    {
                        if(str[0]=='A'&&!a.empty())
                        {
                            printf("%lld
    ",temp[a.top()]);
                            a.pop();
                        }
                        else if(str[0]=='B'&&!b.empty())
                        {
                            printf("%lld
    ",temp[b.top()]);
                            b.pop();
                        }
                        else 
                        {
                            printf("%lld
    ",temp[c.top()]);
                            c.pop();
                        }
                    }
                }
                else 
                {
                    scanf("%s",st);
                    while(!a.empty()||!b.empty())
                    {
    
                        int A,B;
                        if(a.empty())A=0;
                        else A=a.top();
                        if(b.empty())B=0;
                        else B=b.top();
                        if(A>B)
                        {
                            a.pop();
                            d.push(A);
                        }
                        else 
                        {
                            b.pop();
                            d.push(B);
                        }
                    }
                    while(!d.empty())
                    {
                        c.push(d.top());
                        d.pop();
                    }    
                }
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    delphi验证码识别之如何识别高级验证码
    delphi验证码识别学习之图像的灰度化、二值化及反色
    js 数字,金额 用逗号 隔开。数字格式化
    fedora linux 下安装pwntcha[验证码开源]
    C#的多线程机制探索4
    【一天的作息时间】.....程序员们,好好看看
    我的图像之路之CAPTCHA 和 break CAPTCHA
    C#格式化字符串
    struts2拦截器
    java动态代理(JDK和cglib)
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5755076.html
Copyright © 2011-2022 走看看