码迷,mamicode.com
首页 > 其他好文 > 详细

hdu-5818 Joint Stacks(模拟)

时间:2016-08-10 00:43:02      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:

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(‘\n‘);
}
 
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:\n",++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\n",temp[a.top()]);
                        a.pop();
                    }
                    else if(str[0]==‘B‘&&!b.empty())
                    {
                        printf("%lld\n",temp[b.top()]);
                        b.pop();
                    }
                    else 
                    {
                        printf("%lld\n",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;
}

  

hdu-5818 Joint Stacks(模拟)

标签:

原文地址:http://www.cnblogs.com/zhangchengc919/p/5755076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!