uva 11995 栈,队列,优先队列,等基本数据结构的应用与理解

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3146

Problem I

I Can Guess the Data Structure!

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

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

题目大意:给出一系列的操作和返回值,你的任务是猜猜这个”包包“里到底是什么数据结构。有可能是栈,队列,优先队列(数值大的整数优先输出)
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
queue<int>q;
stack<int>s;
priority_queue<int>p;
int n,x,y;
int main()
{
    while(~scanf("%d",&n))
    {
        while(!q.empty())
            q.pop();
        while(!s.empty())
            s.pop();
        while(!p.empty())
            p.pop();
        int flagq=1,flagp=1,flags=1;
        int a;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&x,&y);
            if(x==1)
            {
                q.push(y);
                p.push(y);
                s.push(y);
            }
            else
            {
                if(flagq==1)
                {
                    if(q.empty())
                    {
                        flagq=0;
                    }
                    else
                    {
                        a=q.front();
                        q.pop();
                        if(a!=y)
                            flagq=0;
                    }
                }
                if(flagp==1)
                {
                    if(p.empty())
                    {
                        flagp=0;
                    }
                    else
                    {
                        a=p.top();
                        p.pop();
                        if(a!=y)
                            flagp=0;
                    }
                }
                if(flags==1)
                {
                    if(s.empty())
                    {
                        flags=0;
                    }
                    else
                    {
                        a=s.top();
                        s.pop();
                        if(a!=y)
                            flags=0;
                    }
                }
            }
        }
        if(flagq&&!flagp&&!flags)
        {
            printf("queue\n");
            continue;
        }
        if(!flagq&&flagp&&!flags)
        {
            printf("priority queue\n");
            continue;
        }
        if(!flagq&&!flagp&&flags)
        {
            printf("stack\n");
            continue;
        }
        if(!flagq&&!flagp&&!flags)
        {
            printf("impossible\n");
            continue;
        }
        printf("not sure\n");
    }
    return 0;
}


你可能感兴趣的:(uva 11995 栈,队列,优先队列,等基本数据结构的应用与理解)