Uva 11995 I Can Guess The Data Structure

12 篇文章 5 订阅
7 篇文章 0 订阅

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

#include <bits/stdc++.h>
using namespace std;
int n;
int main()
{
    int a,b;
    while (scanf("%d",&n)!=EOF)
    {
        stack<int>s;
        queue<int>q;
        priority_queue<int,vector<int>,less<int> >p;
        int f[4];
        for(int i=0;i<4;i++)    f[i]=1;
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&a,&b);
            if(a==1)  ///分别进入三种数据结构
            {
                s.push(b);
                q.push(b);
                p.push(b);
            }
            else
            {
                if(!s.empty())
                {
                    if(s.top()!=b)///进栈
                    {
                        f[0]=0;
                    }
                    s.pop();
                }
                else
                    f[0]=0;
                if(!q.empty())///进队
                {
                    if(q.front()!=b)
                    {
                        f[1]=0;
                    }
                    q.pop();
                }
                else
                    f[1]=0;

                if(!p.empty()) ///进优先队列
                {
                    if(p.top()!=b)
                    {
                        f[2]=0;
                    }
                    p.pop();

                }
                else
                    f[2]=0;


            }

        }
        int cnt=0;
            for(int j=0;j<3;j++)
            {
                if(f[j]==1)
                    cnt+=1;
            }
            if(cnt==0)
                cout<<"impossible\n";
            else if(cnt>1)
                cout<<"not sure\n";
            else
            {
                if(f[0])
                    cout<<"stack\n";
                if(f[1])
                    cout<<"queue\n";
                if(f[2])
                    cout<<"priority queue\n";
            }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值