Joint Stacks
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1380 Accepted Submission(s): 659
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 timewhen they were pushed, just like repeating their "push" operations in one stack. See the sample input/outputfor further explanation.
Given two mergeable stacks A and B, implement operations mentioned above.
Input
There are multiple test cases. Foreachcase, the first line contains an integer N(0<N≤105), indicating the numberof operations. The next N lines, each contain an instruction "push", "pop"or"merge". The elements of stacks are32-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 endofinput.
OutputForeachcase, print a line "Case #t:", where t is the casenumber (starting from1). Foreach"pop" operation, output the element that is popped, in a single line.
Sample Input4
push A 1
push A 2
pop A
pop A
9
push A 0
push A 1
push B 3
pop A
push A 2merge A B
pop A
pop A
pop A
9
push A 0
push A 1
push B 3
pop A
push A 2merge B A
pop B
pop B
pop B
0
Sample OutputCase #1:
21Case #2:
1230Case #3:
1230
Author
SYSU
Source
2016 Multi-University Training Contest 7
Joint StacksTime Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1380 Accepted Submission(s): 659Problem DescriptionA stack is a data structure