stack or queue
2017-08-22 14:55
183 查看
3:stack or queue
总时间限制: 1000ms 内存限制: 65535kB描述栈和队列都是常用的线性结构,它们都提供两个操作:
Push:加入一个元素。
Pop:弹出一个元素。
不同的是,栈是”先进后出”,而队列则是”先进先出”。
给出一个线性结构的进出顺序,判定这个结构是栈还是队列。
输入第一行输入一个整数t,代表有t组测试数据
对于每组测试数据,第一行输入一个整数n,代表操作的次数。
随后输入n行,每行包含两个整数 type val。
当type = 1时,表示该次操作为push操作,val表示进入的数字。当type=2时,表示该次操作为pop操作,val代表出来的数字。
3<=n<=2000输出每组测试数据输出一行。
输出改组数据对应的线性结构,”Stack” 或者 “Queue”。
题目保证是栈或者队列的一种。样例输入
261 11 21 32 32 22 141 11 22 12 2
样例输出
StackQueue
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
using namespace std;
string Struct;
bool Hasfind;
int In[2005];
int Out[2005];
int main()
{
int Test;
cin>>Test;
while(Test--)
{
int n,a,b,Innum=0,Outnum=0;
cin>>n;
for(int i=0;i<n;++i)
{
cin>>a>>b;
if(a==1)
{
In[Innum++]=b;
}
else
{
Out[Outnum++]=b;
}
}
int num=min(Innum,Outnum);
Hasfind=false;
for(int i=0;i<num;++i)
{
if(In[i]!=Out[i])
{
Struct="Stack";
Hasfind=true;
break;
}
}
if(!Hasfind) Struct="Queue";
cout<<Struct<<endl;
}
return 0;
}
相关文章推荐
- ZOJ 3210 A Stack or A Queue?
- ZOJ#3210:A Stack or A Queue?
- ZOJ-3210-A Stack or A Queue?【6th浙江省赛】
- The 6th Zhejiang Provincial Collegiate Programming Contest->Problem I:A Stack or A Queue?
- ZOJ 3210 A Stack or A Queue?
- ZOJ 3210 A Stack or A Queue? 水题
- ZOJ 3210 A Stack or A Queue?【模拟】
- 2014ACM集训13级PK赛4-A Stack or A Queue?
- A Stack or A Queue?
- ZOJ 3210 A Stack or A Queue
- (队列的应用5.3.1)ZOJ 3210 A Stack or A Queue?根据进入结构的序列和离开结构的序列确定是stack还是queue)
- 3:stack or queue
- zoj 3210 A Stack or A Queue?
- hdu1702 list or stack+queue
- The 6th Zhejiang Provincial Collegiate Programming Contest->Problem I:A Stack or A Queue?
- stack or queue
- POJ3210 a stack or a queue
- 【数据结构】第2周 栈与队列 3:stack or queue
- A Stack or A Queue?
- ZOJ3210-A Stack or A Queue?