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" (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
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <string>
 5 #include <iomanip>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <vector>
 9 #include <map>
10 #include <stack>
11 using namespace std;
12 #define maxn 110
13 int a[maxn], b[maxn];
14 int T, N, temp, cnt;
15 int main(){
16     scanf("%d", &T);
17     while(T--){
18         stack <int> s;
19         queue <int> q;
20
21         scanf("%d", &N);
22         for(int i = 1; i <= N; i++){
23             scanf("%d", &temp);
24             s.push(temp); q.push(temp);
25         }
26         for(int i = 1; i <= N; i++) scanf("%d", &b[i]);
27
28         bool flagq = true, flags = true;
29         cnt = 1;
30         while(!q.empty()){
31             int temp1 = q.front();
32             if(temp1 != b[cnt]){
33                 flagq = false;break;
34             }
35             q.pop(); cnt++;
36         }
37         cnt = 1;
38         while(!s.empty()){
39             int temp1 = s.top();
40             if(temp1 != b[cnt]){
41                 flags = false;break;
42             }
43             s.pop(); cnt++;
44         }
45         if(flagq && flags) printf("both\n");
46         else if(flagq && !flags) printf("queue\n");
47         else if(!flagq && flags) printf("stack\n");
48         else printf("neither\n");
49
50
51     }
52
53     return 0;
54 }

ZOJ 3210 A Stack or A Queue? (I),布布扣,bubuko.com

时间: 2024-10-29 19:10:41

ZOJ 3210 A Stack or A Queue? (I)的相关文章

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?

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

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 struc

poj 3125 Printer Queue (队列)

 Printer Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3679   Accepted: 1975 Description The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs i

STL之Queue(Q)

STL的Queue(数据结构中的队列): 特点:FIFO 先进先出: 自适应容器(即容器适配器)   栈适配器STL queue  STL中实现的Queue: 用list来实现queue: queue<int, list<int> >  q; 用deque来实现queue: queue<int, deque<int> > q; 不能用vector来实现queue: STL中Queue实现的方法(6种): q.empty(); q.size(); q.fron

UVA10128 - Queue(dp)

题目链接 题目大意:有N个人,通过排序,可以使得从前面往后面看只有P个人,从后面往前面看,只有R个人.问这样的排列有多少种. 解题思路:之前一直在要想怎么排序,结果是方法没找好,看了别人的题接后才发现应该把高个子的先排好,然后再把矮个子的插进去,这样对于从前往后看,还是从后往前看才有规律可循.假设n - 1个人已经排好对了,现在要排第n个人,这个人比之前的n - 1个人都要矮,这样我们就可以有三种排法:(1).排在最前面那么前面看的就多了一个人:(2)排在对尾,那么从后面看的就多了一个人.(3)

POJ 3654 &amp; ZOJ 2936 &amp; HDU 2723 Electronic Document Security(模拟)

题目链接: PKU:http://poj.org/problem?id=3654 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2936 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2723 Description The Tyrell corporation uses a state-of-the-art electronic document system th

『ZOJ 3547』The Boss on Mars (容斥原理)

传送门戳这里qwq 题目描述 On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger boss. Due to no moons around Mars, the employees can only get the salaries per-year. There are nemployees in ACM, and it’s time for them

使用LinkedList实现Stack(栈)与Queue(队列)

首先引用JDK API中关于LinkedList的一句说明:"These operations allow linked lists to be used as a stack, queue, or double-ended queue."由此,可以得知,使用LinkedList可以轻松的实现栈和队列的功能.通过查看LinkedList源码实现,发现其底层采用双向链表实现. 一:使用LinkedList实现Stack public class TestStack{ public sta