I Can Guess the Data Structure! UVA - 11995 (數據結構)

                I Can Guess the Data Structure!

 UVA - 11995 

題目鏈接:https://vjudge.net/problem/UVA-11995

題意:給定N個操作,詢問這些操作能否滿足棧、隊列、優先隊列。

題解:建立棧、隊列、優先隊列,判斷操作是否合法即可,操作可能有錯,所以pop需要判斷empty。

AC代碼:

#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <cstdio>
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;i++)
stack<int> S;
queue<int> Q;
priority_queue<int> P;
int t,n,ans[4];
void intc()
{
    while(Q.size())Q.pop();
    while(P.size())P.pop();
    while(S.size())S.pop();
    _for(i,1,3)ans[i]=0;
}
int main(int argc, char const *argv[])
{
    while(scanf("%d",&n)!=EOF)
    {
        intc();
        _for(i,1,n)
        {
            int x,y;
            cin>>x>>y;
            if(x==1)
            {
                S.push(y);
                P.push(y);
                Q.push(y);
            }
            else
            {
                if(!S.empty())
                {
                    int now = S.top();
                    S.pop();
                    if(now!=y)ans[1]=1;
                }
                else ans[1]=1;
                if(!Q.empty())
                {
                    int now = Q.front();
                    if(now!=y)ans[2]=1;
                    Q.pop();
                }
                else ans[2]=1;
                if(!P.empty())
                {
                    int now=P.top();
                    if(now!=y)ans[3]=1;
                    P.pop();
                }
                else ans[3]=1;
            }
        }
        int h=0;
        _for(i,1,3)if(ans[i]==1)h++;
        if(h<2)cout<<"not sure"<<endl;
        else if(h==3)cout<<"impossible"<<endl;
        else
        {
            if(ans[1]==0)cout<<"stack"<<endl;
            if(ans[2]==0)cout<<"queue"<<endl;
            if(ans[3]==0)cout<<"priority queue"<<endl;
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章