hdu 5818 Joint Stacks 栈



Joint Stacks

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 265    Accepted Submission(s): 100


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
 

Author
SYSU
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5820  5819  5817  5816  5815 
 

Statistic |  Submit |  Discuss |  Note


题意:给出两个栈A、B,n个操作

三种操作:

push  A/B  x (将x放入A/B)

pop A/B (A/B出栈)

merge A B (将A、B栈的元素按照入栈时间顺序放入A中)或merge B A,

要求 每次pop时输出元素的值。

(n<=50000)


解:

对所有入栈的元素编号,

为了保证顺序一致,很容易想到构造另一个栈C。

每次merge时,将上次merge之后A、B栈新加入的元素,全部加到C中(通过编号和vis)

然后清空A、B栈,当需要出栈但是某个栈已经空了时,直接从C中出栈。


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int INF=1e9+7;
const double eps=1e-7;
const int maxn=100000;
typedef long long ll;
int n,na;
char op[15];
bool vis[maxn+10];
ll a[maxn+10];
int st[3][maxn+10],top[3],last;
void push()
{
    char ch;
    scanf(" %c",&ch);
    scanf("%lld",&a[na]);

    int k= ch=='A'?0:1;
    st[k][++top[k]]=na;
     na++;

}

void merge()
{
    char chx,chy;
    scanf(" %c  %c",&chx,&chy);
    int k= chx=='A'?0:1 ;
    top[0]=top[1]=0;


    for(int i=last;i<na;i++) if(!vis[i])
    {
        st[2][++top[2]]=i;
    }
    last=na;

}


void pop()
{
    char ch;
    scanf(" %c",&ch);
    int k= ch=='A'?0:1;
    if( top[k] )
    {
        int x=st[k][top[k]];
        vis[x]=1;
        printf("%lld\n",a[x]);
        top[k]--;
    }
    else
    {
        int x=st[2][top[2]--];
        printf("%lld\n",a[x]);

    }

}
int main()
{
    int kase=0;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0;i<3;i++)
        {
            top[i]=0;
        }
        memset(vis,0,sizeof vis );
        na=last=0;
        printf("Case #%d:\n",++kase);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",op);
            if(strcmp(op,"push")==0)
            {
                push();
            }
            else if(strcmp(op,"merge")==0)
            {
               merge();
            }
            else
            {
               pop();
            }
        }

    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值