Topic Links:
Joint Stacks
Time limit:8000/4000 MS (java/others)
Memory limit:65536/65536 K (java/others)
Problem Descriptiona Stack is a data structure in which all insertions and deletions of entries was made at one end, Calle D the "top" of the stack. The last entry which are inserted is the first one, that'll 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 is 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 would obtain all elements A and B contained before, and B would become empty. The elements in the new stack is rearranged according to the time of they were pushed, just like repeating their "push" Operations in one stack. See the sample Input/output for further explanation.
Given mergeable Stacks A and B, implement operations mentioned above.
Inputthere is multiple test cases. For each case, the first line contains an integer N(0<n≤5), indicating the number of operations. The next N lines, each contain an instruction "push", "pop" or "merge". The elements of stacks is 32-bit integers. Both A and B are empty initially, and it's Guaranteed that "POPs" operation would not being performed to an empty stack. N = 0 Indicates the end of input.
Outputfor, print a line ' case #t: ', where T is the case number (starting from 1). For each "pop" operation, the output of the element is popped.
Sample Input4push a 1push a 2pop Apop a9push a 0push a 1push B 3pop apush a 2merge a bpop Apop Apop a9push a 0push a 1push b 3pop Apush A 2merge b Apop Bpop bpop b 0
Sample outputcase #1:21Case #2:1230Case #3:1230 Test Instructions:
Now there are two stacks, with stacks and stacks and merge operations, asking how many times the number is out of the stack; Idea: Open three stacks, simulation of these operations, when out of the stack to find that the current stack is empty when jumping to the third stack, want to use the chain list simulation but feel bad to write; AC Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define for (i,j,n) for (int i=j;i<=n;i++) #define MST (SS,B) memset (ss,b,sizeof (ss)); typedef long Long LL; Template<class t> void Read (t&num) {char CH; bool F=false; For (Ch=getchar (); ch< ' 0 ' | | Ch> ' 9 '; f= ch== '-', Ch=getchar ()); for (num=0; ch>= ' 0 ' &&ch<= ' 9 '; num=num*10+ch-' 0 ', Ch=getchar ()); F && (num=-num);} int stk[70], tp;template<class t> inline void print (T p) {if (!p) {puts ("0"); return;} while (p) stk[++ TP] = p%10, p/=10; while (TP) Putchar (stk[tp--] + ' 0 '); Putchar (' \ n ');} Const LL Mod=1e9+7;const double Pi=acos ( -1.0); const int INF=1E18;CONST int N=1e5+10;const int Maxn=5e3+4;const double eps= 1e-12;stack<int>a,b,c,d;char S[10],ST[10],STR[10]; LL Temp[n];int Main () {int case=0; LL x; while (1) {int n,cnt=0; Read (n); if (n==0) break; while (!a.empty ()) A.pop (); while (!b.empty ()) B.pop (); while (!c.empty ()) C.pop (); printf ("Case #%d:\n", ++case); for (i,1,n) {scanf ("%s%s", s,str); if (s[0]== ' P ') {if (s[1]== ' u ') {scanf ("%lld", &x); Temp[++cnt]=x; if (str[0]== ' A ') A.push (CNT); else B.push (CNT); } else {if (str[0]== ' A ' &&!a.empty ()) { printf ("%lld\n", Temp[a.top ()]); A.pop (); } else if (str[0]== ' B ' &&!b.empty ()) {printf ("%lld\n", Temp[b.top ()]); B.pop (); } else {priNTF ("%lld\n", Temp[c.top ()]); C.pop (); }}} else {scanf ("%s", ST); while (!a.empty () | |! B.empty ()) {int A, B; if (A.empty ()) a=0; else A=a.top (); if (B.empty ()) b=0; else B=b.top (); if (a>b) {a.pop (); D.push (A); } else {b.pop (); D.push (B); }} while (!d.empty ()) {C.push (D.top ()); D.pop (); }}}} return 0;}
hdu-5818 Joint Stacks (analog)