2016 Summer vacation multi-school joint---Joint Stacks (STL)

Source: Internet
Author: User

HDU 5818

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< Span id= "mathjax-span-2" class= "Mrow" >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 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 ideas: Use C stack to save a, b stack of the merger, you can use a D stack to save A and B Merge and then lead to C stack; The code is as follows:
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<stack>#include<queue>using namespacestd;structnode{intx; intt;}; Stack<Node*>a,b,c,d;intMain () {intN,case=1; Chars[Ten]; Charx; intC;  while(SCANF ("%d", &n) &&N) {printf ("Case #%d:\n", case++);  for(intI=1; i<=n;i++) {scanf ("%s", s); if(s[1]=='u') {scanf ("%c",&x); scanf ("%d",&c); Node* a=NewNode (); A->x=C; A->t=i; if(x=='A') A.push (A); ElseB.push (a); }            Else if(s[1]=='o') {scanf ("%c",&x); if(x=='A')                {                    if(A.empty ()) {printf ("%d\n", C.top ()x);                    C.pop (); }                    Else{printf ("%d\n", A.top ()x);                    A.pop (); }                }                Else                {                    if(B.empty ()) {printf ("%d\n", C.top ()x);                    C.pop (); }                    Else{printf ("%d\n", B.top ()x);                    B.pop (); }                }            }            Else if(s[1]=='e') {scanf ("%c",&x); scanf ("%c",&x);  while(! A.empty () &&!B.empty ()) {                    if(A.top ()->t>b.top ()t) {D.push (A.top ());                    A.pop (); }                    Else{D.push (B.top ());                    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 (); }            }        }    }    return 0;}

2016 Summer School combined---Joint Stacks (STL)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.