题目描述
栈和队列都是常用的线性结构,它们都提供两个操作:
Push:加入一个元素。
Pop:弹出一个元素。
不同的是,栈是”先进后出”,而队列则是”先进先出”。
给出一个线性结构的进出顺序,判定这个结构是栈还是队列。
输入格式
第一行输入一个整数T,代表有T组测试数据,T<=20
对于每组测试数据:
第一行,一个整数n,代表操作的次数。3<=n<=2000
接下来n行,每行包含两个整数 type val。当type = 1时,表示该次操作为push操作,val表示进入的数字。当type=2时,表示该次操作为pop操作,val代表出来的数字。
输出格式
每组测试数据输出一行。
输出改组数据对应的线性结构,”Stack” 或者 “Queue”。
题目保证是栈或者队列的一种。
输入输出样例
输入样例1:
2
6
1 1
1 2
1 3
2 3
2 2
2 1
4
1 1
1 2
2 1
2 2
输出样例1:
Stack
Queue
【耗时限制】1000ms 【内存限制】128MB
//
//Created by Carlgood.
//
//Note:This program is written in version DEV-C++ 5.11.
# include<iostream>
# include<cmath>
# include<string>
# include<cstring>
# include<cstdio>
# include<algorithm>
# include<sstream>
# include<iomanip>
# include<vector>
# include<queue>
# include<deque>
# include<stack>
# include<stdio.h>
# define This_program_is_written_by_Carlgood_Programming_Studio 9876543210
using namespace std;
const int STACK=1,QUEUE=2;
int main()
{
int t,n,command,x,ans;
cin>>t;
while(t--)
{
cin>>n;
stack<int> s;
queue<int> q;
for(int j=1;j<=n;j++)
{
cin>>command>>x;
if(command==1)
{
s.push(x);
q.push(x);
}
else
{
if(s.top()!=x)
{
ans=QUEUE;
}
else if(q.front()!=x)
{
ans=STACK;
}
s.pop();
q.pop();
}
}
if(ans==QUEUE)
{
cout<<"Queue\n";
}
else
{
cout<<"Stack\n";
}
}
return 0;
}