A Stack or A Queue?


Time Limit:
1 Second      Memory Limit: 32768 KB


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 integer
N indicating the number of integers (1 <= N <= 100). The second line of each test case contains
N 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 contains
N 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

4
3
1 2 3
3 2 1
3
1 2 3
1 2 3
3
1 2 1
1 2 1
3
1 2 3
2 3 1

Sample Output

stack
queue
both
neither

水题一道~,主要是考察栈和队列的基本原理,先入先出的是队列,后入先出的是栈~,

主要是对输入和输出进行推断即可了,是否能与他们吻合。

以下是ac的代码:

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=120;
int a[maxn];
int main()
{
int t,n,x;
scanf("%d",&t);
while(t--)
{
bool isqueue=true; //推断是栈还是队列
bool isstack=true;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n;i++)
{
scanf("%d",&x);
if(x!=a[i])
isqueue=false;
if(x!=a[n-i-1])
isstack=false;
}
if(isstack&&isqueue)
printf("both\n");
else if(isqueue)
printf("queue\n");
else if(isstack)
printf("stack\n");
else
printf("neither\n");
}
return 0;
}

zoj 3210 A Stack or A Queue? (数据结构水题)的更多相关文章

  1. ZOJ 3210 A Stack or A Queue?

    A Stack or A Queue? Time Limit: 1 Second      Memory Limit: 32768 KB Do you know stack and queue? Th ...

  2. (队列的应用5.3.1)ZOJ 3210 A Stack or A Queue?根据进入结构的序列和离开结构的序列确定是stack还是queue)

    /* * ZOJ_3210.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...

  3. 从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray)

    从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数 ...

  4. stack, deque 和 queue的对比

    stack, deque 和 queue这三个c++的STL的数据结构很类似但又各有不同. stack是堆栈,没有迭代器,特点是后进先出.用push()将元素压入栈中,top()返回栈顶元素,pop( ...

  5. 使用Stack堆栈集合大数据运算

    使用Stack堆栈集合大数据运算 package com.sta.to; import java.util.Iterator; import java.util.Stack; public class ...

  6. 转:C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、Sort)

    C#常用的集合类型(ArrayList类.Stack类.Queue类.Hashtable类.Sort) .ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在Array ...

  7. 两个stack实现一个queue

    package com.hzins.suanfa; import java.util.Stack; /** * 两个stack实现一个queue * @author Administrator * * ...

  8. STL学习笔记6 -- 栈stack 、队列queue 和优先级priority_queue 三者比较

    栈stack  .队列queue  和优先级priority_queue 三者比较 默认下stack 和queue 基于deque 容器实现,priority_queue 则基于vector 容器实现 ...

  9. ADT基础(一)—— List,Stack,and Queue

    ADT基础(一)-- List,Stack,and Queue 1 List 表示 数组:易于search,难于insert和remove 链表:难于search,易于insert和remove // ...

随机推荐

  1. Directx11学习笔记【十七】纹理贴图

    本文由zhangbaochong原创,转载请注明出处http://www.cnblogs.com/zhangbaochong/p/5596180.html 在之前的例子中,我们实现了光照和材质使得场景 ...

  2. 北邮iptv用WindowsMediaplayer打不开的解决的方法

    前言:之前我的iptv能够用,可是有次我安装了realplayer,它就偷偷把iptv文件的默认打开方式给篡改了,卸载了                  realplayer之后,iptv不能直接用 ...

  3. makefile知识点归纳的

    以一个样例開始,文件文件夹结构例如以下 ---------(当前文件夹)-----------main.c                   |                   |------- ...

  4. Java的结构之美【2】——销毁对象

    先来看一段代码: import java.util.Arrays; import java.util.EmptyStackException; /** * 2014年6月28日09:31:59 * @ ...

  5. php+sqlite cms

    1 phpSQLiteCMS 最新版本 phpSQLiteCMS 2.0.4 http://phpsqlitecms.net/ 2 taoCMS  最新版本 [2.5Beta5下载地址] 需要php ...

  6. javascript实现函数的默认參数值方法

    近期在学python,得益于python中的decorator思路,想到在javascript中參数是不能定义默认值的,可是能够通过decorator给它模拟出来,话不多说,上代码 <!DO ...

  7. WORD中怎样自己主动生成文件夹?

    步骤: 1.输入当做标题的文字 2.将文字设置为标题样式 3.光标放在要加入�文件夹的位置 4.选择插入->引用->索引和文件夹->文件夹->确定

  8. the apple tree

    the apple tree A long time ago, there was a huge apple tree. A little boy loved to come and lay arou ...

  9. Java设计模式菜鸟系列(十四)代理模式建模与实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39856435 代理模式(Proxy):代理模式事实上就是多一个代理类出来,替原对象进行一些 ...

  10. web 环境项目(intellj部署的tomcat) 重启时报 Exception in thread "HouseKeeper" java.lang.NullPointerException (转)

    Exception in thread "HouseKeeper" java.lang.NullPointerException at org.logicalcobwebs.pro ...