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=;
char s[];
char x;
int c;
while(scanf("%d",&N)&&N)
{
printf("Case #%d:\n",Case++);
for(int i=;i<=N;i++)
{
scanf("%s",s);
if(s[]=='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[]=='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[]=='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 ;
}

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

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

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

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

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

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

    2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...

  4. 2016暑假多校联合---To My Girlfriend

    2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...

  5. 2016暑假多校联合---A Simple Chess

    2016暑假多校联合---A Simple Chess   Problem Description There is a n×m board, a chess want to go to the po ...

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

    2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...

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

    原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...

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

    原题链接 Problem Description Given some segments which are paralleled to the coordinate axis. You need t ...

  9. 2016暑假多校联合---GCD

    Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...

随机推荐

  1. Android开发学习之路-使用Handler和Message更新UI

    在Android中,在非主线程中更新UI控件是不安全的,app在运行时会直接Crash,所以当我们需要在非主线程中更新UI控件,那么就需要用到Handler和Message来实现 Demo中,使用到一 ...

  2. lua实现深度拷贝table表

    lua当变量作为函数的参数进行传递时,类似的也是boolean,string,number类型的变量进行值传递.而table,function,userdata类型的变量进行引用传递.故而当table ...

  3. Atitit. 类与对象的存储实现

    Atitit. 类与对象的存储实现 1. 类的结构和实现1 2. 类的方法属性都是hashtable存储的.2 3. Class的分类 常规类(T_CLASS), 抽象类(T_ABSTRACT T_C ...

  4. Atitit Gaussian Blur 高斯模糊 的原理and实现and 用途

    Atitit Gaussian Blur 高斯模糊 的原理and实现and 用途 1.1. 高斯模糊 的原理(周边像素的平均值+正态分布的权重1 1.2. 高斯模糊 的用途(磨皮,毛玻璃效果,背景虚化 ...

  5. iOS-工厂模式

    概述 在前面两章中,分别介绍了简单工厂模式和工厂方法模式,我们知道简单工厂模式的优点是去除了客户端与具体产品的依赖,缺点是违反了“开放-关闭原则”:工厂方法模式克服了简单工厂模式的缺点,将产品的创建工 ...

  6. react7 react 三目运算

    <body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...

  7. python的继承

    继承是面向对象的重要特征之一,继承是两个类或者多个类之间的父子关系,子进程继承了父进程的所有公有实例变量和方法.继承实现了代码的重用.重用已经存在的数据和行为,减少代码的重新编写,python在类名后 ...

  8. Spring(一)第一个示例

    原文链接:http://www.orlion.ga/185/ 一.下载与安装Spring 1.访问https://repo.spring.io/webapp/#/artifacts/browse/tr ...

  9. java中变量运算细节 (2)

    /* 目的:测试变量的运算方式 结果:byte a, b, c; a = b+c; 或者 a = b+10 形如这种形式的算式, 等式的右边的运算结果默认的都是int型的!因为等式右边有变量, 编译器 ...

  10. javaWeb之maven多数据库环境的配置信息

    在使用maven构建的web项目里,不管采用的是什么orm框架,数据库写死了必然不是最灵活的方式.所以通过maven 的buid方式可以动态的分配数据库信息 比如在jdbc.properties中,可 ...