杭电 5818


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<N105), 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
 
  
4 push A 1 push A 2 pop A pop A 9 push A 0 push A 1 push B 3 pop A push A 2 merge A B pop A pop A pop A 9 push A 0 push A 1 push B 3 pop A push A 2 merge B A pop B pop B pop B 0
 

Sample Output
 
  
Case #1: 2 1 Case #2: 1 2 3 0 Case #3: 1 2 3 0

题目大致意思就是模仿栈操作,插入,删除,(先进后出),按插入时间顺序合并

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;
int a[100005];
int b[100005];
int c[100005];
int x[100005];
int main()
{
    int n;
    int cccc=0;
    while(scanf("%d",&n)!=EOF)
    {
        cccc++;
        if(n==0)
            break;
        printf("Case #%d:\n",cccc);
        int i=0,j=0,k=0,t;
        char s[10];
        char cc;
        for(t=0;t<n;t++)
        {
            scanf("%s",s);
            getchar();
            scanf("%c",&cc);
            if(s[1]=='u')
            {
                scanf("%d",&x[t]);
                if(cc=='A')
                {
                    a[i++]=t;
                }
                else if(cc=='B')
                {
                    b[j++]=t;
                }

            }
            else if(s[1]=='o')
            {
                if(cc=='A')
                {
                    if(i==0)
                    {
                        printf("%d\n",x[c[k-1]]);
                        k--;
                    }
                    else
                    {
                        printf("%d\n",x[a[i-1]]);
                        i--;

                    }
                }
                else if(cc=='B')
                {
                    if(j==0)
                    {
                        printf("%d\n",x[c[k-1]]);
                        k--;
                    }
                    else
                    {
                        printf("%d\n",x[b[j-1]]);
                        j--;
                    }
                }

            }
            else if(s[1]=='e')
            {
                getchar();
                scanf("%c",&cc);
                k=merge(a,a+i,b,b+j,c+k)-c;//解题之关键!将a和b中的元素按默认插入的时间顺序排序放在c的第k个元素后面,将输入数据的下标存到数组中的原因就在于此。
               i=0;
                j=0;
            }
        }
    }
    return 0;
}
如果想按自己所需排序,加一个cmp自定义排序函数即可!

猜你喜欢

转载自blog.csdn.net/nucyangjiayun/article/details/52171793
今日推荐