UVA-11995(I Can Guess the Data Structure!)

题目

There is a bag-like data structure, supporting two operations:

1 xThrow an element x into the bag.
2Take out an element from the bag.

Given a sequence of operations with return values, you’re going to guess the data structure. It is a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger elements first) or something else that you can hardly imagine!

Input

There are several test cases. Each test case begins with a line containing a single integer n (1 ≤ n ≤ 1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x. That means after executing a type-2 command, we get an element x without error. The value of x is always a positive integer not larger than 100. The input is terminated by end-of-file (EOF).

Output

For each test case, output one of the following:

stackIt’s definitely a stack.
queueIt’s definitely a queue.
priority queueIt’s definitely a priority queue.
impossibleIt can’t be a stack, a queue or a priority queue.
not sureIt can be more than one of the three data structures mentioned above.

Sample Input

6
1 1
1 2
1 3
2 1
2 2
2 3
6
1 1
1 2
1 3
2 3
2 2
2 1
2
1 1
2 2
4
1 2
1 1
2 1
2 2
7
1 2
1 5
1 1
1 3
2 5
1 4
2 4

Sample Output

queue
not sure
impossible
stack
priority queue

题目大意

有一个容器,可能是栈,队列,优先队列,或者都不是;
有多组样例,第一行表示后面跟着n组数据,后面每组数据由两个数字构成第一个数字为1或2,1表示从容器顶存入第二个数据;2表示从容器顶取出第二个数据
要从给出的存取状况判断容器的类别
栈:stack
队列:queue
优先队列:priority queue
可能是三个中的某一个无法判断:not sure
不是三个中的任何一个:impossible

这里有可能出现没有元素了,但是还取了元素出来,那这肯定就不是三个中的任何一个了,需要输出 impossible
需要进行标记或者其他处理,同时后面就需要首先对impossible 的情况进行判断因为这个有两个分支情况(都是我的泪啊!!!!!)

AC代码

#include<cstdio>
#include<stack>
#include<queue>
using namespace std;
int main()
{
    int n;
    int w1,w2,w3;
    int a,b;
    int v;
    stack<int> x;//定义栈进行模拟
    queue<int> y;//定义队列进行模拟
    priority_queue<int> z;//定义优先队列进行模拟
    while(~scanf("%d",&n))
    {
        w1=0;
        w2=0;
        w3=0;
        v=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d %d",&a,&b);
            if(a==1)//分别向三个模拟的容器顶加入元素
            {
                x.push(b);
                y.push(b);
                z.push(b);
            }
            if(a==2)
            {
                if(x.empty())//当容器内无元素时用v进行标记(表示不是三个元素的任意一个)
                    v=1;
                else//如果容器顶不是要取的元素则分别标记
                {
                    if(x.top()!=b)
                        w1=1;
                    if(y.front()!=b)
                        w2=1;
                    if(z.top()!=b)
                        w3=1;
                    x.pop();
                    y.pop();
                    z.pop();
                }
            }
        }
        if((w1+w2+w3)==3||v==1)//当对应三个容器都被标记时或者v被标记时表示不是三个中的任一个(这个必须放在判断的最前面)
            printf("impossible\n");
        else if((w1+w2+w3)<2)
            printf("not sure\n");
        else if(w1==0)
            printf("stack\n");
        else if(w2==0)
            printf("queue\n");
        else if(w3==0)
            printf("priority queue\n");
        while(!x.empty())
            x.pop();
        while(!y.empty())
            y.pop();
        while(!z.empty())
            z.pop();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值