A Stack or A Queue?
来源:互联网 发布:cae软件 编辑:程序博客网 时间:2024/05/19 12:23
Description
Do you know stack and queue? They're both important data structures. A stack is a "first in last out" (FILO) data structure and a queue is a "first in first out" (FIFO) one.
Here comes the problem: given the order of some integers (it is assumed that the stack and queue are both for integers) going into the structure and coming out of it, please guess what kind of data structure it could be - stack or queue?
Notice that here we assume that none of the integers are popped out before all the integers are pushed into the structure.
Input
There are multiple test cases. The first line of input contains an integer T (T <= 100), indicating the number of test cases. Then T test cases follow.
Each test case contains 3 lines: The first line of each test case contains only one integerN indicating the number of integers (1 <= N <= 100). The second line of each test case containsN integers separated by a space, which are given in the order of going into the structure (that is, the first one is the earliest going in). The third line of each test case also containsN integers separated by a space, whick are given in the order of coming out of the structure (the first one is the earliest coming out).
Output
For each test case, output your guess in a single line. If the structure can only be a stack, output "stack"; or if the structure can only be a queue, output "queue"; otherwise if the structure can be either a stack or a queue, output "both", or else otherwise output "neither".
Sample Input
431 2 33 2 131 2 31 2 331 2 11 2 131 2 32 3 1
Sample Output
stackqueuebothneither
#include <stdio.h>int main(){ int a[101],b[101]; int n,i,j,k; scanf("%d",&n); while(n--) { int bs = 3,bq = 3; scanf("%d",&k); for(i = 0;i<k;i++) scanf("%d",&a[i]); for(i = 0;i<k;i++) scanf("%d",&b[i]); int x,y,z; for(x = 0;x<k;x++) { if(a[x] != b[x]) { bq = 0; break; } } if(x>=k) bq = 1; y = k-1; for(x = 0;x<k;x++) { if(a[x] != b[y]) { bs = 0; break; } y--; } if(x >= k) bs = 1; if(bs == 0 && bq == 1) printf("queue\n"); else if(bs == 1 && bq == 0) printf("stack\n"); else if(bs == 1 && bq == 1) printf("both\n"); else printf("neither\n"); } return 0;}
- A Stack or A Queue?
- A Stack or A Queue?
- A Stack or A Queue?
- A Stack or A Queue?
- A Stack or A Queue?
- A Stack or A Queue?
- ZOJ 3210 A Stack or A Queue
- zoj 3210 A Stack or A Queue?
- A Stack or A Queue?(zoj 3210)
- ZOJ 3210 A Stack or A Queue?
- D - A Stack or A Queue?
- ZOJ 3210 A Stack or A Queue?
- ZOJ 3210 A Stack or A Queue?
- zoj3210 A Stack or A Queue?
- POJ3210 a stack or a queue
- ZOJ 3210 A Stack or A Queue?
- ZOJ#3210:A Stack or A Queue?
- ZOJ 3210 A Stack or A Queue ?
- 关于ios中得路径详细讲解
- 黑马程序员——银行业务调度管理系统
- Harris角点检测
- CDZSC_2014上半年组队赛(1)
- java中equals方法和“==”的比较
- A Stack or A Queue?
- CSS中需要注意的地方
- eclipse failed to create the java virtual machine 问题图文解析
- __stdcall 与 __cdecl 区别 (汇编 call ret 时,栈的变化)
- eclipse安装插件
- 大道至简(原标题:少是指数级的多)
- 如何深入JVM
- DAO模式
- Lua5.2新版本可变长参数操作