2016多校第七场1010(hdu5818)

106 篇文章 0 订阅
7 篇文章 0 订阅


Joint Stacks

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 57    Accepted Submission(s): 17


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
 

这题的题意是两个栈进行pop、push、merge操作,前两种操作和普通的栈一样,merge操作是把两个栈按它们入栈的顺序合并一起,注意这里的入栈顺序是指加入一个栈的顺序,至于是那个栈并没有影响,为了方便合并,入栈的时候最好忽略入哪一个栈,所以可以想到,我们只需要开一个栈来记录,也就是开一个结构体数组模拟,记录它的状态:入的是a栈还是b栈,或者已经出栈。然后当需要merge操作时,用两个标记变量,mergx记录当前的栈顶,mergz记录合并到哪个栈。然后在pop操作时,都需要判断下栈顶是在标记变量之前还是之后,之前正常输出,之后就把接下来的元素都当做是在mergz栈中处理,复杂度应该接近o(n),代码量也小。


代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct p
{
    int x;
    char na;
} a[501234];
int b[501234];
int main()
{
    int n;


    int k=1;

    while(~scanf("%d", &n))
    {
        if(n==0)break;
        int top=0;
        int num=0;
        int merx=-1;
        char merz;
        int i, j;
        char ml[12];
        int e;
        for(e=0; e<n; e++)
        {
            scanf("%s", ml);
            if(strcmp(ml, "push")==0)
            {
                char x,q;
                int y;
                scanf("%c%c%d", &q, &x, &y);
                a[top].x=y;
                a[top++].na=x;
            }
            if(strcmp(ml, "pop")==0)
            {
                char x, q;
                scanf("%c%c", &q, &x);
                for(i=top-1; i>=0; i--)
                {
                    if(i>merx)
                    {
                        if(a[i].na==x)
                        {
                         a[i].na=0;
                         b[num++]=a[i].x;
                         break;
                        }

                    }
                    else
                    {
                        if(merz==x)
                        {
                            if(a[i].na!=0)
                            {
                              a[i].na=0;
                              b[num++]=a[i].x;
                              break;
                            }
                        }

                    }
                }
            }
            if(strcmp(ml, "merge")==0)
            {
                char q, x, y, r;
                scanf("%c%c%c%c", &q, &x, &r, &y);
                merx=top-1;
                merz=x;
            }
        }
        printf("Case #%d:\n", k++);
        for(i=0; i<num; i++)printf("%d\n", b[i]);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值