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?