#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;
}
hdu 5818
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.