题目链接:hdu 5818
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 < N ≤ 105), 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
比赛时候这个题不是我写的(我也就写不出来……)。
后来试图自己强行水过(链表),然后,RE, T个不停……TAT
然后就搜了下题解,官方题解:
……被cin打败的人生→
我感觉我水不过和cin关系很大啊!
于是改scanf
但是不知道是什么姿势问题,自己改了改,查了几个题解学着改了改,RE……Orz
居然是重名了。
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAX 300005
#define mod 1000000007
#define INF -0x3f3f3f3f
using namespace std;
struct node
{
int p, num;
};
node a[MAX], b[MAX];
int c[MAX];
int main()
{
int N, t, cnt = 1, prea, preb, prec, id;
while(scanf("%d", &N) && N)
{
printf("Case #%d:\n", cnt);
prea = preb = prec = 0;
id = 1;
while(N--)
{
char ch[12], ch1[5], cc[5];
scanf("%s%s", ch, ch1);
if(ch[1] == 'u')
{
scanf("%d", &t);
if(ch1[0] == 'A')
{
a[prea].num = t;
a[prea++].p = id++;
}
else
{
b[preb].num = t;
b[preb++].p = id++;
}
}
else if(ch[1] == 'o')
{
if(ch1[0] == 'A')
{
if(!prea) printf("%d\n", c[--prec]);
else printf("%d\n", a[--prea].num);
}
else
{
if(!preb) printf("%d\n", c[--prec]);
else printf("%d\n", b[--preb].num);
}
}
else if(ch[1] == 'e')
{
scanf("%s", cc);
int ta = 0, tb = 0;
while(ta < prea && tb < preb) c[prec++] = a[ta].p < b[tb].p ? a[ta++].num : b[tb++].num;
while(ta < prea) c[prec++] = a[ta++].num;
while(tb < preb) c[prec++] = b[tb++].num;
prea = preb = 0;
}
}
cnt++;
}
return 0;
}
运行结果: