Summer Practice Race 004 E Joint Stacks

Source: Internet
Author: User

Joint Stackscrawling in process ... crawling failed time limit:4000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64u

Submit Status Practice HDU 5818 Udebug

Description

Input

Output

Sample Input

Sample Output

Hint

Description

A stack is a data structure in which all insertions and deletions of entries be made at one end, called 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.

Input

There is multiple test cases. For each case, the first line contains an integer, 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.

Output

For each case, 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 Input

4push 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 Apus H A 2merge b Apop Bpop bpop b 0

Sample Output

Case #1:21Case #2:1230Case #3:1230
* * Tonight a lot of worries, was a problem card E for a long time, absent-minded bar * *
#include <stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<string>#include<queue>#defineN 50010using namespacestd;structnode{intnum; intID; BOOL operator< (ConstNode &other)Const { returnid<other.id; }};p Riority_queue<node>A;priority_queue<node>B;priority_queue<node>C;/*Be sure to use three priority queues here, because when you repeat the merge B a merge a B operation will be timed out (the amount of data is too large) with three priority queue, it will be able to solve the problem perfectly, as long as the merger is merged into C no matter how the operation, will not be sorted */intN;Charstr[Ten];//CommandintMain () {//freopen ("In.txt", "R", stdin); intp=1; while(~SCANF ("%d", &n)!=eof&&N) { while(!a.empty ()) A.pop (); while(!b.empty ()) B.pop (); while(!c.empty ()) C.pop (); printf ("Case #%d:\n", p++); //memset (a,0,sizeof a); //memset (b,0,sizeof b); for(intI=0; i<n;i++) {scanf ("%s", &AMP;STR);//input Command//cout<< "str=" <<str<<endl; Charch; intsh; if(strcmp (str,"Push")==0) { //cout<< "First command" <<endl;scanf"%*c%c%d",&ch,&sh); GetChar (); //cout<<ch<< "" <<sh<<endl; if(ch=='A') {node fr; Fr.num=sh; Fr.id=i; A.push (FR); } Else if(ch=='B') {node fr; Fr.num=sh; Fr.id=i; B.push (FR); } //cout<<str<< "" <<ch<< "" <<sh<<endl; } Else if(strcmp (str,"Pop")==0) {scanf ("%*c%c",&ch); GetChar (); if(ch=='A') { if(A.empty ()) {cout<<c.top () .num<<Endl; C.pop (); } Else{cout<<a.top () .num<<Endl; A.pop (); } } Else if(ch=='B') { if(B.empty ()) {cout<<c.top () .num<<Endl; C.pop (); } Else{cout<<b.top () .num<<Endl; B.pop (); } } //cout<<str<< "" <<ch<<endl; } Else if(strcmp (str,"Merge")==0) { Chars1,s2; scanf ("%*c%c%c",&s1,&S2); GetChar (); while(!A.empty ()) {Node Fr=A.top (); A.pop (); C.push (FR); } while(!B.empty ()) {Node Fr=B.top (); B.pop (); C.push (FR); } //cout<<str<< "" <<s1<< "" <<s2<<endl; } } } return 0;}

Summer Practice Race 004 E Joint Stacks

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.