2016暑假多校联合---Joint Stacks (STL)

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

思路:用C栈保存A、B栈的合并,可以再用一个D栈保存A和B的合并,然后再导到C栈;

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
using namespace std;
struct Node
{
    int x;
    int t;
};
stack<Node*>A,B,C,D;

int main()
{
    int N,Case=1;
    char s[10];
    char x;
    int c;
    while(scanf("%d",&N)&&N)
    {
        printf("Case #%d:\n",Case++);
        for(int i=1;i<=N;i++)
        {
            scanf("%s",s);
            if(s[1]==‘u‘)
            {
                scanf(" %c",&x);
                scanf("%d",&c);
                Node* a=new Node();
                a->x=c;
                a->t=i;
                if(x==‘A‘)
                    A.push(a);
                else
                    B.push(a);
            }
            else if(s[1]==‘o‘)
            {
                scanf(" %c",&x);
                if(x==‘A‘)
                {
                    if(A.empty())
                    {
                        printf("%d\n",C.top()->x);
                        C.pop();
                    }
                    else
                    {
                        printf("%d\n",A.top()->x);
                        A.pop();
                    }
                }
                else
                {
                    if(B.empty())
                    {
                        printf("%d\n",C.top()->x);
                        C.pop();
                    }
                    else
                    {
                         printf("%d\n",B.top()->x);
                         B.pop();
                    }
                }
            }
            else if(s[1]==‘e‘)
            {
                scanf(" %c",&x);
                scanf(" %c",&x);
                while(!A.empty()&&!B.empty())
                {
                    if(A.top()->t>B.top()->t)
                    {
                        D.push(A.top());
                        A.pop();
                    }
                    else
                    {
                        D.push(B.top());
                        B.pop();
                    }
                }
                while(!A.empty())
                {
                    D.push(A.top());
                        A.pop();
                }
                while(!B.empty())
                {
                    D.push(B.top());
                        B.pop();
                }
                while(!D.empty())
                {
                    C.push(D.top());
                    D.pop();
                }
            }
        }
    }
    return 0;
}
时间: 2024-12-22 14:02:54

2016暑假多校联合---Joint Stacks (STL)的相关文章

2016暑假多校联合---Windows 10

2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently, our old monk found the operating system of his computer was updating to windows 10 automatically and he even can't j

2016暑假多校联合---Substring(后缀数组)

2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings. But ?? thinks that is too easy, he wants to make this problem more interesti

2016暑假多校联合---Rikka with Sequence (线段树)

2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has an array A with n numbers. Then he make

2016暑假多校联合---Another Meaning

2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two meanings. Such as “hehe”, which not only means “hehe”, but also means “excuse me”. Today, ?? is chating with MeiZi online, MeiZi sends a sentence A to

2016暑假多校联合---Death Sequence(递推、前向星)

原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian living in 1st century. He and his 40 comrade soldiers were trapped in a cave, the exit of which was blocked by Romans. They chose suicide over captu

2016暑假多校联合---Counting Intersections

原题链接 Problem Description Given some segments which are paralleled to the coordinate axis. You need to count the number of their intersection. The input data guarantee that no two segments share the same endpoint, no covered segments, and no segments

2016暑假多校训练参赛感想

参赛感想 这是第一次参加暑假多校训练,应该也会是人生中最后一次,我真的很庆幸能参加这个训练,和全国几乎所有高校的ACMer一起在一个平台上做题!昨天为止多校已经完全结束,今天看到叉姐的训练感想(叉姐的感想链接),我觉得我也有必要写下自己的训练感想. 人的眼界总是狭窄的,当在自己的学校站在前几名的时候觉得自己还不错,应该会有不错的将来,但是当第一次参加国赛(2015 南阳站)的时候我便被别人实力所震撼,我突然觉得自己在别人的眼里简直就是小学生,菜到不行.别人在5个小时可以AK,而我连最水的题也要想

暑假练习赛 004 E Joint Stacks

Joint StacksCrawling in process... Crawling failed Time Limit:4000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 5818 uDebug Description Input Output Sample Input Sample Output Hint Description A stack is a d

2015暑假多校联合---CRB and His Birthday(01背包)

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son.She went to the nearest shop with M Won(currency unit).At the shop, there are N kinds of pr