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. ZOJ - 3822 Domination (DP)

    Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess ...

  2. 国内PaaS概述和EEPlat定位

    2014国内云计算产业进入快速发展阶段.热火多年来,所以云计算的云计算产业迅速进入栈桥的应用.IaaS.PaaS.SaaS各大厂商具有较强的市场布局,所以,云计算应用在这三个层次的访问,以实际使用阶段 ...

  3. XMPP得知--建立一个管理类

    参考其他demo之后,设立一个管理类的发现看起来更舒服,理-- 但在建立与server连接其中.发现 Connect Error: {     NSLocalizedDescription = &qu ...

  4. jQuery照片伸缩效应,这不是一个简单的图像缩放,它不影响其它元素的布局

    之前在网上看到这样的效果,但我没有收藏夹网址,后来被我不知道如何来实现这种效果. 如今,互联网已收集有关专门.真是功夫不负有心人,被我发现. 我也努力过自己尝试着写: 但仅仅是单纯的图片放大.并且还影 ...

  5. Swing之JTable的详细介绍(转)

    表格(Table)的使用与介绍8-1:使用JTable组件:类层次结构图:java.lang.Object--java.awt.Component--java.awt.Container--javax ...

  6. 解决Nginx的connect() to 127.0.0.1:8080 failed (13: Permission denied) while connect

    在进行Nginx+Tomcat 负载均衡的时候遇到了这个权限问题,在error.log日志中.我们能够看到例如以下: connect() to 127.0.0.1:8080 failed (13: P ...

  7. 利用__index和__newindex实现默认值表、监控表、只读表(转)

    __index和__newindex实际上相当于是在读写表的时候分别加了一道过滤的逻辑,让读写表的操作可以被监控或说回调,利用这个特性可以实现一些带有特殊功能的表. 带有默认值的表: setdefau ...

  8. 寒假了,想深入学习c++

    本来在图书馆借了好几本属,但是,自己没有经验,借的书都太深奥,看不懂,哎,桑心!

  9. hdu 3572 Task Schedule (dinic算法)

    pid=3572">Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  10. ACdreamoj1110(多重背包)

    意甲冠军:多个裸露的双肩背包.水的问题. 解决方法:然背包一样,仅仅只是加一个数组,记录着每一个物品用过的次数,多于存储量时就pass不更新. 另一种方法是将每一个物品用二进制压缩处理.第一个代码比較 ...