UVA: I Can Guess the Data Structure!(stl模拟)

题目链接UVA - 11995

题意:给你一串序列并告诉进出顺序,要你判断这串序列是在什么数据结构中,有stack、queue、priority queue或者有多种可能或者都不可能

思路:直接把进去的序列用stack、queue、priority queue进行模拟,比较出来的序列和给定序列,记录符合条件的个数,再判断即可

AC代码

#include<iostream>
#include<stack>
#include<queue>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        queue<int>q;
        priority_queue<int>p;
        stack<int>s;
        int flag[3]={0};
        for(int i=0; i<n; i++)
        {
            int type,x;
            cin>>type>>x;
            if(type==1)
            {
                s.push(x);
                q.push(x);
                p.push(x);
            }
            if(type==2)
            {
                if(!flag[0] && !s.empty())
                {
                    int y=s.top();
                    s.pop();
                    if(y!=x) flag[0]=1;
                }
                else flag[0]=1;
                if(!flag[1] && !q.empty())
                {
                    int y=q.front();
                    q.pop();
                    if(y!=x) flag[1]=1;
                }
                else flag[1]=1;
                if(!flag[2] && !p.empty())
                {
                    int y=p.top();
                    p.pop();
                    if(y!=x) flag[2]=1;
                }
                else flag[2]=1;
            }
        }
        int cnt=0;
        for(int i=0; i<3; i++)
        {
            if(flag[i]==0) cnt++;
        }
        if(cnt==0) cout<<"impossible"<<endl;
        else if(cnt==1)
        {
            if(flag[0]==0) cout<<"stack"<<endl;
            else if(flag[1]==0) cout<<"queue"<<endl;
            else if(flag[2]==0) cout<<"priority queue"<<endl;
        }
        else cout<<"not sure"<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值