ZOJ 3210 a Stack or a Queue?

Source: Internet
Author: User

 I- A Stack or a Queue?Time limit:MS Memory Limit:32768KB 64bit IO Form At:%lld &%llu Submit Status Practice ZOJ 3210

Description

Do you know stack and queue? They ' re both important data structures. A stack is a ' first in the 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's assumed that the stack and queue be both for integers) go ing into the structure and coming off of it, please guess what kind of data structure it could be-stack or queue?

Notice that this assume that none of the integers is popped out before all the integers is pushed into the structure.

Input

There is multiple test cases. The first line of input contains an integer t (t <=), indicating the n Umber of test cases. then Ttest Cases follow.

Each test case contains 3 Lines:the first line of all test case contains only one integer N I ndicating the number of integers (1 <= N<= 100). The second line of all test case contains N integers separated by a space, which is given in The order of going into the structure (which is, the first one was the earliest going in). The third line of all test case also contains N integers separated by a space, Whick is giv En in the order of coming out of the structure (the first one was the earliest coming out).

Output

For each test case, the output your guess in a single line. IF The structure can is only a stack, output "stack"; Or if the structure can is only 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

431 2 33 2 131 2 31 2 331 2 11 2 131 2 32 3 1

Sample Output

Stackqueuebothneither


Water problem, stack and queue to deal with it just fine,

#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream > #include <algorithm>using namespace Std;int ans;int b[205]; #include <queue> #include <stack>int    Main () {char s[305][305];    Char s1[305];    int i,j;    int n,m;    int ans=0;    int t;    int flag,flag1;    cin>>t;        while (t--) {queue<int>q;        Stack<int >Q;        int n;        cin>>n;            for (i=0; i<n; i++) {scanf ("%d", &m);            Q.push (m);        Q.push (m);        } flag=1;        Flag1=1;            for (i=0; i<n; i++) {scanf ("%d", &m);            if (M!=q.front ()) flag=0;            if (M!=q.top ()) flag1=0;            Q.pop ();        Q.pop ();        } if (flag &&flag1) {cout<< "both" <<endl; } else if (flag &&!flag1) {cout<< "qUeue "<<endl;        } else if (!flag &&flag1) {cout<< "stack" <<endl;    } else cout<< "neither" <<endl; }}


ZOJ 3210 a Stack or a Queue?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.