uva 11995 I Can Guess the Data Structure!

本文通过模拟队列、优先队列及栈三种数据结构的行为,根据输入操作序列判断实际使用的是哪种数据结构。若无法确定则输出不确定或不可能。

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

思路;分别模拟三种数据结构,观察输出是否对应


#include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
int main()
{
    //freopen("in.txt","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        //printf("%d",n);
        int f1=1,f2=1,f3=1;
        queue <int >q1;
        priority_queue<int>q2;
        stack<int> q3;
        int x,y;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            if(x==1)
            {
                q1.push(y);
                q2.push(y);
                q3.push(y);
            }
            else if(x==2)
            {
                int tmp;
                if(!q1.empty())
                {
                    tmp=q1.front();
                    q1.pop();
                    if(tmp!=y)
                        f1=0;
                }
                else
                    f1=0;
                if(!q2.empty())
                {
                    tmp=q2.top();
                    q2.pop();
                    if(tmp!=y)
                        f2=0;
                }
                else
                    f2=0;
                if(!q3.empty())
                {
                   tmp=q3.top();
                    q3.pop();
                    if(tmp!=y)
                        f3=0;
                }
                else
                    f3=0;

            }
        }
        if(f1&&!f2&&!f3)
            printf("queue\n");
        else if(!f1&&f2&&!f3)
            printf("priority queue\n");
        else if(!f1&&!f2&&f3)
            printf("stack\n");
        else if(!f1&&!f2&&!f3)
            printf("impossible\n");
        else
            printf("not sure\n");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值