常用数据结构 UVa 11995:I Can Guess the Data Structure!(数据结构练习) 复习下常用数据结构

版权声明:抱最大的希望,为最大的努力,做最坏的打算。 https://blog.csdn.net/qq_37748451/article/details/86604680
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

题意:

这道题的题目是“猜猜数据结构”,题意就是给你一些输入输出数据,让你根据这些数据判断是什么数据结构。要猜的数据结构只有三种,栈(stack)、队列(queue)、优先队列(priority_queue)。输出有5种情况,前三种分别是确定了一种数据结构,第四种是三种数据结构都不符合,第五种是有2种或2种以上符合。

思路:就是在程序中定义这三种数据结构,根据输入数据,产生各自的输出结果,分别与给定的输出输出对比。如果与测试数据不同,则这种数据结构不可能。最后记录下符合的数据结构的个数,分情况判断输出即可。

主要想通过这个题复习一下常用的数据结构;

栈:后进先出,只允许在一端进行操作

队列:先进先出,类似于排队

优先队列:基于堆的完全二叉树   用来插入元素或者删除最大元素来使用的

#include <iostream>
#include <queue>
#include <stack>
using namespace std;
int main()
{
    int i,n;
    while(cin>>n){
        queue <int> q;
        priority_queue <int> pq;
        stack <int> s;
        bool f[3] = {0};
        for(i=1;i<=n;i++){
            int a,b;
            cin>>a>>b;
            if(a==1){    //入
                q.push(b);
                pq.push(b);
                s.push(b);
            }
            else{    //出
                //依次对比
                if(!f[0] && !q.empty()){
                    int x = q.front();
                    q.pop();
                    if(x!=b) f[0]=true;
                }
                else f[0]=true;
                
                
                if(!f[1] && !pq.empty()){
                    int x = pq.top();
                    pq.pop();
                    if(x!=b) f[1]=true;
                }
                else f[1]=true;

                if(!f[2] && !s.empty()){
                    int x = s.top();
                    s.pop();
                    if(x!=b) f[2]=true;
                }
                else f[2]=true;
            }
        }
        //查找有几个符合输出
        int num=0;
        for(i=0;i<3;i++)
            if(!f[i])
                num++;
        if(num==0)
            cout<<"impossible"<<endl;
        else if(num==1){
            if(!f[0])
                cout<<"queue"<<endl;
            else if(!f[1])
                cout<<"priority queue"<<endl;
            else if(!f[2])
                cout<<"stack"<<endl;
        }
        else 
            cout<<"not sure"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37748451/article/details/86604680