杭电 5818

来源:互联网 发布:成人网络教育培训机构 编辑:程序博客网 时间:2024/06/06 01:03

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 integerN(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
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

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

#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自定义排序函数即可!

0 0
原创粉丝点击