uva 11995 棧,佇列,優先佇列,等基本資料結構的應用與理解

life4711發表於2014-08-11

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;
}


相關文章