ZOJ 3210 A Stack or A Queue ? 水

C - A Stack or A Queue?

Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%lld & %llu

SubmitStatus

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 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


ACcode:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a[110],b[110];
int n,ans;
void fun(){
    ans=0;
    bool flag1=true,flag2=true;
    for(int i=0;i<n;++i){
        if(a[i]!=b[i])flag1=false;
        if(b[i]!=a[n-1-i])flag2=false;
    }
    if(flag1&&flag2){
        printf("both\n");
        return;
    }
    if(flag1){
        printf("queue\n");
        return;
    }
    if(flag2){
        printf("stack\n");
        return;
    }
    printf("neither\n");
}
int main(){
    int loop;
    cin>>loop;
    while(loop--){
        cin>>n;
        for(int i=0;i<n;++i)cin>>a[i];
        for(int i=0;i<n;++i)cin>>b[i];
        fun();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 17:07:24

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? 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" (

ZOJ 3210 A Stack or A Queue? (I)

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" (FIF

ZOJ 3210: A Stack or A Queue?

A Stack or A Queue? ///@author Sycamore, ZJNU ///@date 2017-02-09  #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int T, N; cin >> T; while (T--) { cin >> N; vector<int>v(N); f

【C++】Stack类与Queue类学习

1.Stack类学习 1)建立stack<string> 2)调用push函数将数据压入栈中 3)调用size函数查看当前栈内元素数量 4)调用empty函数检测栈是否为空 5)如果不为空则不断调用pop函数将元素从栈中取出(后入先出) #include <iostream> #include <stack> using namespace std; int main() {     stack<string> stkNameList;     stkNam

stack, deque 和 queue的对比

stack, deque 和 queue这三个c++的STL的数据结构很类似但又各有不同. stack是堆栈,没有迭代器,特点是后进先出.用push()将元素压入栈中,top()返回栈顶元素,pop()移除栈顶元素. deque是双端队列,支持迭代器,使用push_back()在队尾添加元素,pop_back()移除队尾元素,这些跟vector差不多.不同的地方在于deque还可在队首添加和移除元素,使用pop_front()和push_front(). queue是队列,特点是先进先出,不支持

两个stack实现一个queue

package com.hzins.suanfa; import java.util.Stack; /** * 两个stack实现一个queue * @author Administrator * */ public class TwoStackToQueue { private Stack<Integer> stack1; private Stack<Integer> stack2; public TwoStackToQueue(){ stack1 = new Stack<

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

从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数组)控制,map中每个槽指向一个固定大小的缓冲(连续的线性空间). deque的迭代器,关键的四个指针: cur //所指缓冲区中的现元素 first //所指缓冲区中的头 last //所指缓冲区中的尾 node //指向中控的槽 start指向中控第一个槽位的buffer中的第一个元素,fini

C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类)

1.ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在ArrayList中主要使用Add.Remove.RemoveAt.Insert四个方法对栈进行操作.Add方法用于将对象添加到 ArrayList 的结尾处:Remove方法用于从 ArrayList 中移除特定对象的第一个匹配项:RemoveAt方法用于移除 ArrayList 的指定索引处的元素:Insert方法用于将元素插入 ArrayList 的指定索引处. 示例 ArrayList的使用 示例将介

STL之stack容器和queue容器

摘要:本文主要介绍了两种容器——stack容器和queue容器. 1.基本概念   stack容器 queue容器 容器介绍 stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口, 形式如图所示.stack容器允许新增元素,移除元素,取得栈顶元素,但是除了 最顶端外,没有任何其他方法可以存取stack的其他元素.换言之,stack不允 许有遍历行为. 有元素推入栈的操作称为:push,将元素推出stack的操作称为pop. Queue是一种先进先出(