杭电 5818
来源:互联网 发布:成人网络教育培训机构 编辑:程序博客网 时间:2024/06/06 01:03
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.
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 integerN(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
4push A 1push A 2pop Apop A9push A 0push A 1push B 3pop Apush A 2merge A Bpop Apop Apop A9push A 0push A 1push B 3pop Apush A 2merge B Apop Bpop Bpop B 0
Sample Output
Case #1:21Case #2:1230Case #3:1230
题目大致意思就是模仿栈操作,插入,删除,(先进后出),按插入时间顺序合并
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int a[100005];int b[100005];int c[100005];int x[100005];int main(){ int n; int cccc=0; while(scanf("%d",&n)!=EOF) { cccc++; if(n==0) break; printf("Case #%d:\n",cccc); int i=0,j=0,k=0,t; char s[10]; char cc; for(t=0;t<n;t++) { scanf("%s",s); getchar(); scanf("%c",&cc); if(s[1]=='u') { scanf("%d",&x[t]); if(cc=='A') { a[i++]=t; } else if(cc=='B') { b[j++]=t; } } else if(s[1]=='o') { if(cc=='A') { if(i==0) { printf("%d\n",x[c[k-1]]); k--; } else { printf("%d\n",x[a[i-1]]); i--; } } else if(cc=='B') { if(j==0) { printf("%d\n",x[c[k-1]]); k--; } else { printf("%d\n",x[b[j-1]]); j--; } } } else if(s[1]=='e') { getchar(); scanf("%c",&cc); k=merge(a,a+i,b,b+j,c+k)-c;//解题之关键!将a和b中的元素按默认插入的时间顺序排序放在c的第k个元素后面,将输入数据的下标存到数组中的原因就在于此。 i=0; j=0; } } } return 0;}如果想按自己所需排序,加一个cmp自定义排序函数即可!
0 0
- 杭电 5818
- 杭电
- 杭电
- 杭电
- 杭电OJ 5818 Joint Stacks
- 杭电 1234 和 杭电 2115
- 杭电2056之Rectangles 杭电
- 杭电ACM1061Rightmost Digit
- 杭电2099 7.11
- 杭电ACM 1003
- 杭电 ACM 2016
- 杭电ACM1466
- 杭电ACM1003
- 杭电ACM1225
- 杭电ACM2023
- 杭电ACM2602
- 杭电ACM2955
- 杭电ACM3198
- Mac Parallels Dsektop 安装redhat虚拟机并设置ssh登录
- iOS UI篇- 退出键盘
- HDU 5810(伯努利实验、多校7、规律)
- shell expect 教程
- iOS 图形编程总结
- 杭电 5818
- Android 使用PopupWindow实现下拉列表
- 求二叉树的深度
- 2.Redis数据类型
- 多线程编程学习笔记-海量数据求和
- Android动态加载(插件化)基本原理总结
- tcp拥塞控制
- Nginx配置详解
- 在Visual Studio中编译Linux的一些问题