UVA 11995 I Can Guess the Data Structure!

题目大意: 

直接见白书。



解题思路:

简单的STL应用,直接上代码。

小结一下:

1.stack: top(),push();

2.queue: front(),push();

3.priority_queue:top(),push();

4.使用之前一定要判断!empty();


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
stack<int> st;
queue<int> q;
priority_queue<int> pr;
void init()
{
    while(!st.empty())
    {
        st.pop();
    }
    while(!q.empty())
    {
        q.pop();
    }
    while(!pr.empty())
    {
        pr.pop();
    }
}
int main()
{
    int n;
    int a,b;

    while(scanf("%d",&n)==1)
    {
        init();
        int f1=1,f2=1,f3=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d %d",&a ,&b);
            if(a==1)
            {
                st.push(b);
                q.push(b);
                pr.push(b);
            }
            else if(a==2)
            {
                int c;

                if( (!st.empty()) && b==st.top())
                {
                    st.pop();
                }
                else
                {
                    f1=0;
                }

                if( (!q.empty()) && b==q.front())
                {
                    q.pop();
                }
                else
                {
                    f2=0;
                }

                if( (!pr.empty()) && b==pr.top())
                {
                    pr.pop();
                }
                else
                {
                    f3=0;
                }

            }
        }

        int cnt=f1+f2+f3;
        if(cnt>=2)
        {
            printf("not sure\n");
        }
        else if(cnt==0)
        {
            printf("impossible\n");
        }
        else
        {
            if(f1)
            {
                printf("stack\n");
            }
            else if(f2)
            {
                printf("queue\n");
            }
            else if(f3)
            {
                printf("priority queue\n");
            }
        }


    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值