3:stack or queue

来源:互联网 发布:淘宝哪些属于其他来源 编辑:程序博客网 时间:2024/06/05 12:36
总时间限制: 
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<stdio.h>#include<string.h>#include<algorithm>#include<assert.h>#include<ctype.h>#include<stack>using namespace std;int main(){    int t;    cin>>t;    while(t--)    {   stack<int>ce;        bool ok=true;        int n;        cin>>n;        while(n--)        {   int type,val;            scanf("%d%d",&type,&val);            if(type==1)ce.push(val);            if(type==2&&!ce.empty()){                if(val!=ce.top())ok=false;              if(val==ce.top())ce.pop();            }        }        if(ok==false)printf("Queue\n");        else printf("Stack\n");    }    return 0;}

0 0