UVA - 11995 - I Can Guess the Data Structure!(栈、队列、优先队列)

UVA 11995.I Can Guess the Data Structure!

Description

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

operation meaning
1 x Throw an element x into the bag.
2 Take 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). The size of input file does not exceed 1MB.

Output

For each test case, output one of the following:
stack
It's definitely a stack.
queue
It's definitely a queue.
priority queue
It's definitely a priority queue.
impossible
It can't be a stack, a queue or a priority queue.
not sure

It 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

Output for the Sample Input

queue
not sure
impossible
stack
priority queue

给你一组操作,让你判断这组操作对应的那个bag是栈、队列还是优先队列

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;

int main()
{
    int n;
    while(scanf("%d", &n)!=EOF){
        int si = 1, qi = 1, pi = 1;
        stack<int>s;
        queue<int>q;
        priority_queue<int>p;

        while(n--){
            int a, b;
            scanf("%d %d", &a, &b);
            if(a == 1){
                s.push(b);
                q.push(b);
                p.push(b);
            }
            else{
                if(!s.empty() && s.top() == b)  s.pop();
                else    si = 0;

                if(!q.empty() && q.front() == b)       q.pop();
                else    qi = 0;

                if(!p.empty() && p.top() == b)    p.pop();
                else    pi = 0;
            } 
        }

        if(si+qi+pi >= 2)   printf("not sure\n");
        else if(si)     printf("stack\n");
        else if(qi) printf("queue\n");
        else if(pi) printf("priority queue\n");
        else if(!si && !qi && !pi)  printf("impossible\n");
    }
    return 0;
}

你可能感兴趣的:(栈,队列,优先队列,uva)