HDU 5818 Joint Stacks 模拟

来源:互联网 发布:java实现密钥进行加密 编辑:程序博客网 时间:2024/05/16 11:52

题目描述:

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

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 Apush A 2merge B Apop Bpop Bpop B 0

Sample Output

Case #1:21Case #2:1230Case #3:1230

题目分析:

hdu多校第七场的1010。
题目大意是有两个栈A和B,对这两个栈进行入栈、出栈和合并操作。
这里最主要的就是合并两个栈的操作,合并后的栈中元素要求是按照总体的入栈顺序排列的。因此,我们在记录这个元素的时候还要记录他们的入栈时间,在合并的时候开辟一个新的栈C,将合并后的栈放入C中,出栈时如果A栈和B栈已经空,从C栈出栈,这样会减少多次合并后的操作。

代码如下:

#include <bits/stdc++.h>#include <cstdio>#include <cstring>using namespace std;typedef pair <int,int> p;stack <p> a,b,c,d;int n;int cnt;int main(){    cnt=0;    while(scanf("%d",&n)!=-1 && (n!=0))    {        printf("Case #%d:\n",++cnt);        while (!a.empty()) a.pop();        while (!b.empty()) b.pop();        while (!c.empty()) c.pop();        while (!d.empty()) d.pop();        char tmp[7],c1,c2;        int num;        for(int i=1; i<=n; i++)        {            cin>>tmp>>c1;            if (strcmp(tmp,"push")==0)            {                scanf("%d",&num);                p p1=make_pair(num,i);                if (c1=='A') a.push(p1);                else if (c1=='B') b.push(p1);            }            else if (strcmp(tmp,"pop")==0)            {                int x;                if (c1=='A')                {                    if(!a.empty())                    {                        x=a.top().first;                        a.pop();                    }                    else                    {                        x=d.top().first;                        d.pop();                    }                }                else if (c1=='B')                {                    if(!b.empty())                    {                        x=b.top().first;                        b.pop();                    }                    else                    {                        x=d.top().first;                        d.pop();                    }                }                printf("%d\n",x);            }            else if (strcmp(tmp,"merge")==0)            {                cin>>c2;                while(!a.empty() && !b.empty())                {                    int aa=a.top().second;                    int bb=b.top().second;                    if (aa>bb)                    {                        c.push(a.top());                        a.pop();                    }                    else                    {                        c.push(b.top());                        b.pop();                    }                }                while(!a.empty())                {                    c.push(a.top());                    a.pop();                }                while(!b.empty())                {                    c.push(b.top());                    b.pop();                }                while(!c.empty())                {                    d.push(c.top());                    c.pop();                }            }        }    }    return 0;}
0 0
原创粉丝点击