uva11995 - I Can Guess the Data Structure!

本文通过一题解析,展示了如何利用STL中的stack、queue和priority_queue三种数据结构来判断输入序列是否符合特定的数据结构操作。通过对比每次操作后的数据结构状态,可以确定输入序列是否能由单一数据结构完成,或者无法确定,甚至不可能实现。

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

链接

https://vjudge.net/problem/UVA-11995

题解

做完这题我只想说STL大法好
就是我直接模拟三种数据结构,然后判断一下pop的时候pop出来的元素是不是和它的一样就行了

代码

#include <bits/stdc++.h>
#define maxn 1010
using namespace std;
int n;
int main()
{
    int type, x, t1, t2, t3;
    while(~scanf("%d",&n))
    {
        stack<int> s;
        queue<int> q;
        priority_queue<int> pq;
        bitset<3> b;
        b.set();
        while(n--)
        {
            scanf("%d%d",&type,&x);
            if(type==1)
            {
                s.emplace(x);
                q.emplace(x);
                pq.emplace(x);
            }
            else
            {
                t1 = t2 = t3 = -1;
                if(!s.empty())t1=s.top(), s.pop();
                if(!q.empty())t2=q.front(), q.pop();
                if(!pq.empty())t3=pq.top(), pq.pop();
                if(t1!=x)b.reset(0);
                if(t2!=x)b.reset(1);
                if(t3!=x)b.reset(2);
            }
        }
        if(b.count()==0)
        {
            printf("impossible\n");
        }
        else if(b.count()==1)
        {
            if(b.test(0))
            {
                printf("stack\n");
            }
            if(b.test(1))
            {
                printf("queue\n");
            }
            if(b.test(2))
            {
                printf("priority queue\n");
            }
        }
        else
        {
            printf("not sure\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值