UVA11995 I Can Guess the Data Structure!

思路

简单题,用栈,队列,优先队列直接模拟即可

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
queue<int> q;
stack<int> S;
priority_queue<int> pq;
int n;
int main(){
    while(scanf("%d",&n)==1){
        while(!q.empty())
            q.pop();
        while(!S.empty())
            S.pop();
        while(!pq.empty())
            pq.pop();
        bool isS=true,isq=true,ispq=true;
        for(int i=1;i<=n;i++){
            int opt,x;
            scanf("%d %d",&opt,&x);
            if(opt==1){
                q.push(x);
                pq.push(x);
                S.push(x);
            }
            else{
                if(q.empty())
                    isq=false;
                if(pq.empty())
                    ispq=false;
                if(S.empty())
                    isS=false;
                if(isq){
                    int t=q.front();
                    if(t!=x)
                        isq=false;
                    q.pop();
                }
                if(ispq){
                    int t=pq.top();
                    if(t!=x)
                        ispq=false;
                    pq.pop();
                }
                if(isS){
                    int t=S.top();
                    if(t!=x)
                        isS=false;
                    S.pop();
                }
            }
        }
        if((!ispq)&&(!isq)&&(!isS))
            printf("impossible\n");
        else if((ispq)&&(!isq)&&(!isS))
            printf("priority queue\n");
        else if((!ispq)&&(isq)&&(!isS))
            printf("queue\n");
        else if((!ispq)&&(!isq)&&(isS))
            printf("stack\n");
        else
            printf("not sure\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/dreagonm/p/10681208.html