uva 11995 I Can Guess the Data Structure! (STL의 간단한 응용)

3920 단어 STL


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

제목: 두 가지 작업을 지원하는 "패키지"와 유사한 데이터 구조가 있습니다.
1a. 요소 a를 가방에 넣는다.
2a가방에서 a를 꺼낸다
이 가방이 무엇을 대표하는지 일련의 조작을 보여 주십시오.창고일 수도 있고, 대기열일 수도 있고, 우선 대기열 (수치가 큰 선출) 이나 다른 것일 수도 있다.
분석: 문제는 어렵지 않다. 한 가지 문제를 주의해야 한다.'가방'이 비어 있을 때 가방에서 물건을 찾을 수 없다.
코드:
#include
#include
#include
using namespace std;
int main()
{
    int n, i, k, a, f[4];
    while(~scanf("%d",&n))
    {
        stack s;
        queue q;
        priority_queue, less > pq;
        for(i = 0; i < 3; i++) f[i] = 1;
        for(i = 0; i < n; i++)
        {
            scanf("%d%d",&k,&a);
            if(k == 1)
            {
                s.push(a);
                q.push(a);
                pq.push(a);
            }
            else
            {
                if(!s.empty())
                {
                    if(s.top() != a)
                        f[0] = 0;
                    s.pop();
                }
                else
                    f[0] = 0;

                if(!q.empty())
                {
                    if(q.front() != a)
                        f[1] = 0;
                    q.pop();
                }
                else
                    f[1] = 0;

                if(!pq.empty())
                {
                    if(pq.top() != a)
                        f[2] = 0;
                    pq.pop();
                }
                else
                    f[2] = 0;
            }
        }
        int cnt = 0;
        for(i = 0; i < 3; i++)
            if(f[i] == 1)
                cnt++;
        if(cnt == 0)
            printf("impossible
"); else if(cnt > 1) printf("not sure
"); else { if(f[0] == 1) printf("stack
"); else if(f[1] == 1) printf("queue
"); else printf("priority queue
"); } } return 0; }


좋은 웹페이지 즐겨찾기