hdu 5818

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <map>
#include <queue>
#include <stdlib.h>
#include <cmath>
#include <vector>
#include <stack>

#define clr(x) memset(x,0,sizeof(x))
#define clr2(x) memset(x,INF,sizeof(x))
#define clr3(x) memset(x,-INF,sizeof(x))
#define INF 0x3f3f3f3f
#define MAXN 100010
#define MAXM 100010
#define pb(x) push_back(x)
#define ll long long
#define ull unsigned long long

using namespace std;



int belong;
int num[MAXN];
int Case=0;
int n,idt;
char obj[10],obj2[10];
int vis[MAXN];
struct Node
{
    int num,id;
} tt;

stack<Node> a,b,c;
void PUSH()
{
    scanf("%s%d",obj,&tt.num);
    tt.id=idt;

    if (obj[0]=='A')
        a.push(tt);
    else
        b.push(tt);
    idt++;
}

void POP()
{
    scanf("%s",obj);

    if (obj[0]=='A')
    {
        if (!a.empty())
        {
            Node temp=a.top();
            a.pop();
            printf("%d\n",temp.num);
        }
        else
        {
            Node temp=c.top();
            c.pop();
            printf("%d\n",temp.num);
        }
    }
    else
    {
        if (!b.empty())
        {
            Node temp=b.top();
            b.pop();
            printf("%d\n",temp.num);
        }
        else
        {
            Node temp=c.top();
            c.pop();
            printf("%d\n",temp.num);
        }
    }
}
stack <Node> d;
void MERGE()
{
    scanf("%s%s",obj,obj2);
    while ( !a.empty() && !b.empty() )
    {
        Node ta=a.top();
        Node tb=b.top();
        if (ta.id>tb.id)
        {
            d.push(ta);
            a.pop();
        }
        else
        {
            d.push(tb);
            b.pop();
        }
    }
    while (!a.empty())
    {
        d.push(a.top());
        a.pop();
    }
    while (!b.empty())
    {
        d.push(b.top());
        b.pop();
    }

    while (!d.empty())
    {
        c.push(d.top());
        d.pop();
    }
}

void solve()
{
    char s[11];
    clr(vis);

    printf("Case #%d:\n",++Case);
    for (int i=1; i<=n; i++)
    {
        scanf("%s",s);
        if (s[0]=='p' && s[1]=='u')
            PUSH();

        if (s[0]=='p' && s[1]=='o')
            POP();
        if (s[0]=='m')
            MERGE();
    }
}
int main()
{

    while (scanf("%d",&n) && n)
        solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值