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. Atiti 重定向标准输出到字符串转接口adapter stream流体系 以及 重定向到字符串

    Atiti 重定向标准输出到字符串转接口adapter stream流体系 以及 重定向到字符串 原理::syso  向ByteArrayOutputStream这个流理想write字节..然后可以使 ...

  2. 《鸟哥的linux私房菜》 - linux命令温故而知新

    在公司的某角落里,看到了<鸟哥的linux私房菜>,顿时想看看是什么鬼. 其他时候还要自己去买才有,现在正好,比图书馆方便.看完了,写点啥! 编辑器很重要,一个vim就主要是我的使用方向: ...

  3. vue.js学习之入门实例

    之前一直看过vue.js官网api,但是很少实践,这里抽出时间谢了个入门级的demo,记录下一些知识点,防止后续踩坑,牵扯到的的知识点:vue.vue-cli.vue-router.webpack等. ...

  4. fir.im Weekly - 技术人也要苦练“七十二变”

    一年又一年,Code,Build,Run.多少技术人像"孙悟空"一样,日复一日苦练"七十二变",笑对"八十一难",最后能"取经成功 ...

  5. MongoDB修改器总结

    1"$set":用来制定一个字段值,若不存在,则创建:    一般用于点加一个字段   db.users.update({name:"joe"},{" ...

  6. 【资源】C++学习资料 - 逆天整理 - 精华无密版【最新】

    再失效就太无语了,链接都是多份的~~—————————————————基础——————————————C++环境搭建(全套)http://pan.baidu.com/s/1o6y0smY链接:http ...

  7. java中TreeSet集合如何实现元素的判重

    /* 看一下部分的TreeSet源码.... public class TreeSet<E> extends AbstractSet<E> implements Navigab ...

  8. 【原创】.NET读写Excel工具Spire.Xls使用(5)重量级的Excel图表功能

                  本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html .NET读写Excel工具Spire.Xls使用文章 ...

  9. python--基础学习(六)sqlite数据库基本操作

    python系列均基于python3.4环境 1.新建数据表 新建表,命名为student(id, name, score, sex, age),id为关键字,代码如下: import sqlite3 ...

  10. tomcat本地部署war包的方式

    最近学习java,在本地tomcat部署时,主要采取以下方式: 一.本地构建生成war包 在本地项目中的terminal下,使用命令 “mvn clean install”或 先执行“mvn clea ...