2016HDU多校联赛-HDU-Joint Stacks(模拟)

本文介绍了一种特殊的数据结构——可合并栈,并通过示例详细解释了如何实现其基本操作,包括入栈、出栈及两个栈的合并。重点在于合并操作,即按元素入栈时间顺序合并两个栈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Joint Stacks

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

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

题意:三种操作,入栈,出栈(同时输出),合并(按照入栈时间先后顺序合并)

思路:数组模拟,入栈和合并时都标记一下,可以做到O(1)操作,输出时回溯输出,由于栈的特性,总的复杂度为O(N)

代码

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=1e5+10;
struct node
{
    int num;
    int flag;//0代表A,1代表B,-1代表已输出
} num[maxn];
int main()
{
    int N;
    int casen=1;
    while(~scanf("%d",&N)&&N)
    {
        printf("Case #%d:\n",casen++);
        int point_num=0;//num的指针
        for(int i=0; i<N; i++)
        {
            char str1[7];//判断push,pop,merge
            char str2[2];//接收A或B
            int flag_num;//接收数字
            scanf("%s",str1);
            if(str1[1]=='u')//push
            {
                scanf("%s%d",str2,&flag_num);
                num[point_num].num=flag_num;
                num[point_num].flag=(int)(str2[0]-'A');
                point_num++;
            }
            else if(str1[1]=='o')//pop
            {
                //这里输出要注意了
                scanf("%s",str2);
                //2代表后面全是A,3代表后面全是B
                int len_num=point_num-1;
                while(!(num[len_num].flag==(int)(str2[0]-'A')||num[len_num].flag==(int)(str2[0]-'A'+2)))
                    len_num--;
                if(num[len_num].flag==(int)(str2[0]-'A'))
                {
                    printf("%d\n",num[len_num].num);
                    num[len_num].flag=-1;
                }
                else if(num[len_num].flag==(int)(str2[0]-'A'+2))
                {
                    printf("%d\n",num[len_num].num);
                    int flag_len=len_num-1;
                    while(num[flag_len].flag==-1)
                        flag_len--;
                    num[flag_len].flag=(int)(str2[0]-'A'+2);
                    num[len_num].flag=-1;//标记为已输出
                }
                while(num[point_num-1].flag==-1)
                    point_num--;
            }
            else if(str1[1]=='e')//merge
            {
                char str3[2];
                scanf("%s%s",str2,str3);
                num[point_num-1].flag=(int)(str2[0]-'A'+2);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值