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
水题一道~,主要是考察栈和队列的基本原理,先入先出的是队列,后入先出的是栈~,
主要是对输入和输出进行推断即可了,是否能与他们吻合。
以下是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? (数据结构水题)的更多相关文章
- 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 ...
- (队列的应用5.3.1)ZOJ 3210 A Stack or A Queue?根据进入结构的序列和离开结构的序列确定是stack还是queue)
/* * ZOJ_3210.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...
- 从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray)
从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数 ...
- stack, deque 和 queue的对比
stack, deque 和 queue这三个c++的STL的数据结构很类似但又各有不同. stack是堆栈,没有迭代器,特点是后进先出.用push()将元素压入栈中,top()返回栈顶元素,pop( ...
- 使用Stack堆栈集合大数据运算
使用Stack堆栈集合大数据运算 package com.sta.to; import java.util.Iterator; import java.util.Stack; public class ...
- 转: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 // ...
随机推荐
- Directx11学习笔记【十八】 Blending混合
本文由zhangbaochong原创,转载请注明出处http://www.cnblogs.com/zhangbaochong/p/5634580.html 在d3d11中是按frame来渲染物体的,在 ...
- Java 的布局管理器GridBagLayout的使用方法(转)
GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求 ...
- Cocos2d-x学习笔记(六) 定时器Schedule的简单应用
Cocos2d-x中的定时器使用非常easy,共同拥有3种:schedule.scheduleUpdate和scheduleOnce.简介一下三种的差别: schedule,每隔指定时间运行某个 ...
- 实验数据结构——KMP算法Test.ming
翻译计划 小明初学者C++,它确定了四个算术.关系运算符.逻辑运算.颂值操作.输入输出.使用简单的选择和循环结构.但他的英语不是很好,记住太多的保留字,他利用汉语拼音的保留字,小屋C++,发明 ...
- RH133读书笔记(2)-Lab 2 Working with packages
Lab 2 Working with packages Goal: To gain working experience with package management System Setup: A ...
- CloudFoundry 中的GoRouter性能測试
之前一直感觉CloudFoundry的GoRouter的性能不靠谱,或者我们的CloudFoundry 部署架构存在问题,想着进行一些压力測试,可是一直苦于没有压力測试的工具.上一周,部门须要出一个測 ...
- 关于WCF的引用,添加服务和添加web服务的区别
原文:关于WCF的引用,添加服务和添加web服务的区别 本章内容主要是根据我做的实验来阐述这2种添加服务针对WCF的不同之处,我们按照示例一步一步来看. 如下是工程的结构: 该WCF服务是通过控制台程 ...
- Mongodb 之insert瞬时完成,测试数据---飞天博客
这几天看mongdb官方网站,然后将执行数据.突然,我发现,该数据确实很强大,在这里说话数据.我用普通的pc机,amd双核 2.7GHz,4G内存,当然,当系统不只是在测试作为数据库server的.同 ...
- C++中的class (2)
class Father { protected void methodA(){ //do something } private void methodB(){//do something } } ...
- TextWatcher原因activity内存泄漏问题
TextWatcher原因activity内存泄漏. EditText配置addTextChangedListener该接口,至onDestroy电话里removeTextChangedListene ...