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? 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#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string> using namespace std;
int a[105];
int b[105];
int n;
int main()
{
int t;
scanf("%d",&t);
int tag1;
int tag2;
while(t--)
{
scanf("%d",&n);
tag1=1;tag2=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
for(int i=1;i<=n;i++)
{
if(a[i]!=b[i])
{
tag1=0;
}
if(a[i]!=b[n-i+1])
{
tag2=0;
} }
if(tag1&&tag2)
{
printf("both\n");
}
else if(tag1&&!tag2)
{
printf("queue\n");
}
else if(!tag1&&tag2)
{
printf("stack\n");
}
else
{
printf("neither\n");
} }
return 0;
}
ZOJ 3210 A Stack or A Queue?的更多相关文章
- 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? ...
- (队列的应用5.3.1)ZOJ 3210 A Stack or A Queue?根据进入结构的序列和离开结构的序列确定是stack还是queue)
/* * ZOJ_3210.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...
- stack, deque 和 queue的对比
stack, deque 和 queue这三个c++的STL的数据结构很类似但又各有不同. stack是堆栈,没有迭代器,特点是后进先出.用push()将元素压入栈中,top()返回栈顶元素,pop( ...
- 从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray)
从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数 ...
- 转:C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、Sort)
C#常用的集合类型(ArrayList类.Stack类.Queue类.Hashtable类.Sort) .ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在Array ...
- 两个stack实现一个queue
package com.hzins.suanfa; import java.util.Stack; /** * 两个stack实现一个queue * @author Administrator * * ...
- STL学习笔记6 -- 栈stack 、队列queue 和优先级priority_queue 三者比较
栈stack .队列queue 和优先级priority_queue 三者比较 默认下stack 和queue 基于deque 容器实现,priority_queue 则基于vector 容器实现 ...
- ADT基础(一)—— List,Stack,and Queue
ADT基础(一)-- List,Stack,and Queue 1 List 表示 数组:易于search,难于insert和remove 链表:难于search,易于insert和remove // ...
- The 6th Zhejiang Provincial Collegiate Programming Contest->Problem I:A Stack or A Queue?
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3210 题意:给出stack和queue的定义,一个是先进后出(FILO), ...
随机推荐
- PNG透明兼容IE6的几种方法(转)
png 透明针对 IE6 一直是件挺麻烦的事情,使用的方法也是各有不同,大多的原理是用 IE 的滤镜来解决的. 语法: filter:progid:DXImageTransform.Microsoft ...
- Javascript农历与公历相互转换
/**用法 * Lunar.toSolar(2016, 6, 3); 农历转化公历 * Lunar.toLunar(2016, 7, 6); 公历转化农历 */ var Lunar = { MIN_Y ...
- perl readlink 函数-返回软链接指向的文件
readlink 函数专门用于处理链接,可以返回该链接指向的文件的路径 代码示例: print readlink($prog) if (-f $prog && -l $prog):
- js中数组作为参数传递的定义
下面的函数实现了一个我们想要的最基本的图片预加载效果 function preloadimages(arr){ var newimages=[] var arr=(typeof arr!= ...
- DLL文件的使用
一. 动态链接库 什么是动态链接库?DLL三个字母对于你来说一定很熟悉吧,它是Dynamic Link Library 的缩写形式,动态链接库 (DLL) 是作为共享函数库的可执行文件.动态链接提供了 ...
- MathType编辑指数的方法
利用MathType编辑公式使得在文档中编辑理工类的论文工作减轻了不少,它所包含的符号与模板基本都可以满足我们日常工作学习中对公式的需要.在文档中编辑数学物理符号或者是函数表达式,都是用word公式编 ...
- CSS 解决IE6双倍距离BUG
只要满足下面3个条件才会出现这个BUG: 1)要为块状元素: 2)要左侧浮动: 3)要有左外边距(margin-left): 解决这个BUG很容易,只需要在相应的块状元素的 ...
- vs2013\2015-UML
1.UML简介Unified Modeling Language (UML)又称统一建模语言或标准建模语言. 简单说就是以图形方式表现模型,根据不同模型进行分类,在UML 2.0中有13种图,以下是他 ...
- Android中使用UncaughtExceptionHandler来处理未捕获的异常
原文在sparkyuan.me上.转载注明出处:http://sparkyuan.github.io/2016/03/28/使用UncaughtExceptionHandler来处理未捕获的异常/ 全 ...
- Android权限全记录(转)
常用权限: 读写存储卡装载和卸载文件系统 android.permission.WRITE_EXTERNAL_STORAGE android.permission.READ_EXTERNAL_STOR ...