UVA11995----数据结构水题

本文解析了一个UVA在线评测平台上的数据结构题目,通过模拟stack、queue和priority queue的操作,判断输入序列对应的最可能的数据结构类型。利用C++ STL进行实现,并考虑了特殊情况的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

这个题目的意思很简单,给你一些push和pop操作,然后根据操作的结果来猜数据结构。

就三种,

stack

queue

priority queue

正好用STL就可以模拟。

但是模拟的时候要注意,在pop的时候,STL中的可能已经为空了。所以要加个判断进去。

下面看我的代码:

#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
using namespace std;

stack<int> a;
queue<int> b;
priority_queue<int> c;

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int x,y;
        while(!a.empty())
            a.pop();
        while(!b.empty())
            b.pop();
        while(!c.empty())
            c.pop();

        bool aa,bb,cc;
        aa=bb=cc=true;
        while(n--)
        {
            scanf("%d%d",&x,&y);
            if(x==1)
            {
                a.push(y);
                b.push(y);
                c.push(y);
            }
            else
            {
                if(!a.empty())
                {
                    int aaa = a.top();
                    a.pop();
                    if(aaa!=y)
                        aa=false;
                }
                else
                    aa=false;

                if(!b.empty())
                {
                    int bbb = b.front();
                    b.pop();
                    if(bbb!=y)
                        bb=false;
                }
                else
                    bb=false;

                if(!c.empty())
                {
                    int ccc = c.top();
                    c.pop();
                    if(ccc!=y)
                        cc=false;
                }
                else
                    cc=false;
            }
        }
        if((aa&&bb) || (aa&&cc) || (bb&&cc))
            printf("not sure\n");
        else if(aa)
            printf("stack\n");
        else if(bb)
            printf("queue\n");
        else if(cc)
            printf("priority queue\n");
        else
            printf("impossible\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值