题目链接:

Joint Stacks

Time Limit: 8000/4000 MS (Java/Others)  

  Memory Limit: 65536/65536 K (Java/Others)

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
 
题意:

现在有两个栈,有入栈和出栈和合并的操作,问每次出栈的数字是多少;
 
思路:
 
开三个栈,模拟这几种操作,当出栈时发现当前栈为空时就跳到第三个栈,
想用链表模拟可是感觉不好写;
 
AC代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e18;
const int N=1e5+10;
const int maxn=5e3+4;
const double eps=1e-12; stack<int>a,b,c,d; char s[10],st[10],str[10]; LL temp[N];
int main()
{
int Case=0;
LL x;
while(1)
{
int n,cnt=0;
read(n);
if(n==0)break;
while(!a.empty())a.pop();
while(!b.empty())b.pop();
while(!c.empty())c.pop();
printf("Case #%d:\n",++Case);
For(i,1,n)
{
scanf("%s%s",s,str);
if(s[0]=='p')
{
if(s[1]=='u')
{
scanf("%lld",&x);
temp[++cnt]=x;
if(str[0]=='A')a.push(cnt);
else b.push(cnt);
}
else
{
if(str[0]=='A'&&!a.empty())
{
printf("%lld\n",temp[a.top()]);
a.pop();
}
else if(str[0]=='B'&&!b.empty())
{
printf("%lld\n",temp[b.top()]);
b.pop();
}
else
{
printf("%lld\n",temp[c.top()]);
c.pop();
}
}
}
else
{
scanf("%s",st);
while(!a.empty()||!b.empty())
{ int A,B;
if(a.empty())A=0;
else A=a.top();
if(b.empty())B=0;
else B=b.top();
if(A>B)
{
a.pop();
d.push(A);
}
else
{
b.pop();
d.push(B);
}
}
while(!d.empty())
{
c.push(d.top());
d.pop();
}
}
}
}
return 0;
}

  

hdu-5818 Joint Stacks(模拟)的更多相关文章

  1. HDU 5818 Joint Stacks(联合栈)

    HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. HDU 5818 Joint Stacks (优先队列)

    Joint Stacks 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5818 Description A stack is a data stru ...

  3. HDU 5818 Joint Stacks

    Joint Stacks Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  4. hdu 5818 Joint Stacks (优先队列)

    Joint Stacks Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. HDU 5818 Joint Stacks ——(栈的操作模拟,优先队列)

    题意:有两个栈A和B,有3种操作:push,pop,merge.前两种都是栈的操作,最后一种表示的是如果“merge A B”,那么把B中的元素全部放到A中,且满足先入后出的栈原则. 分析:显然,我们 ...

  6. HDU - 5818 Joint Stacks 比较大の模拟,stack,erase

    https://vjudge.net/problem/HDU-5818 题意:给你两个栈AB,有常规push,pop操作,以及一个merge操作,merge A B 即将A.B的元素按照入栈顺序全部出 ...

  7. HDU 5818 Joint Stacks(左偏树)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5818 [题目大意] 给出两个栈A B(初始时为空),有三种操作: push.pop.merge. ...

  8. HDU 5818:Joint Stacks(stack + deque)

    http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description   A stack is a data ...

  9. 暑假练习赛 004 E Joint Stacks(优先队列模拟)

    Joint StacksCrawling in process... Crawling failed Time Limit:4000MS     Memory Limit:65536KB     64 ...

随机推荐

  1. curl库pycurl实例及参数详解

    pycurl是功能强大的python的url库,是用c语言写的,速度很快,比urllib和httplib都快. 今天我们来看一下pycurl的用法及参数详解 常用方法: pycurl.Curl() # ...

  2. git stash 保存当前工作状态

    1. git stash   暂存当前工作状态 2. git stash list 查看暂存列表 3. git stash save 'title' 暂存工作状态并添加说明 4. git stash ...

  3. 处理字符串的一些C函数

    注意:以下函数都包含在ctype.h头文件中 1.isalpha函数 用来判断得到的参数是不是字母 #include<stdio.h> #include<ctype.h> in ...

  4. 前端标签--js--css大致思路

    html标签语言在块级和内联标签的基础上进行页面的设计,设计的时候主要是注意标签块间的距离位置等信息,设计盒子的浮动,盒子的位置,盒子之间的联系. 在设计网页之前一定要判断好该设计多少个盒子,什么样的 ...

  5. python之脚本参数optparse

    import optparse usage = "myprog[ -f <filename>][-s <xyz>] arg1[,arg2..]" opter ...

  6. python爬虫获取百度图片(没有精华,只为娱乐)

    python3.7,爬虫技术,获取百度图片资源,msg为查询内容,cnt为查询的页数,大家快点来爬起来.注:现在只能爬取到百度的小图片,以后有大图片的方法,我会陆续发贴. #!/usr/bin/env ...

  7. sphinx PDF 中文

    使用reST撰写文档时,需要分多个文档时,就必须使用sphinx了,sphinx说起来很简单的,但是默认是不是支持中文的.幸好我出生的晚,sphinx现在已经支持xelatex了^_^ 安装 除了pa ...

  8. 完好用户体验: 活用window.location与window.open解决页面跳转问题

    原文地址: JavaScript Redirects and window.open原文日期: 2014年08月27日翻译日期: 2014年08月31日翻译人员: 铁锚 (译者注: 本文解决的是按 C ...

  9. java 面试总结

    1.static变量与实体变量的差别? static是静态变量,static能够通过类名直接訪问 内存方面的不同:static在定义的时候jvm就会分配空间, 而实体变量仅仅有在创建对象的时候才会去分 ...

  10. 设置Eclipse中properties文件打开方式myeclipse一样有source和properties两个视图方法

    东北大亨: 说明:如果想在eclipse的properties文件打开的方式出现source和properties视图就需要添加JBossTools插件 下面介绍如果添加插件: 1.打开官网 http ...