UVA 11995(p186)----I Can Guess the Data Structure!

#include<bits/stdc++.h>
#define debu
using namespace std;
int n,flag[3];
stack<int> s;
queue<int> q;
priority_queue<int,vector<int>,less<int> >pq;
void enter(int x)
{
    s.push(x);
    q.push(x);
    pq.push(x);
}
int output(int i)
{
    int tmp;
    if(i==0)
    {
        if(!s.empty())
        {
            tmp=s.top();
            s.pop();
        }
    }
    else if(i==1)
    {
        if(!q.empty())
        {
            tmp=q.front();
            q.pop();
        }
    }
    else
    {
        if(!pq.empty())
        {
            tmp=pq.top();
            pq.pop();
        }
    }
    return tmp;
}
void print()
{
    int num=0;
    for(int i=0; i<3; i++)
        if(flag[i]) num++;
    if(num>1)
        printf("not sure\n");
    else if(num==0)
        printf("impossible\n");
    else
    {
        if(flag[0]) printf("stack\n");
        if(flag[1]) printf("queue\n");
        if(flag[2]) printf("priority queue\n");
    }
}
void init()
{
    memset(flag,1,sizeof(flag));
    while(!s.empty()) s.pop();
    while(!q.empty()) q.pop();
    while(!pq.empty()) pq.pop();
}
int main()
{
#ifdef debug
    freopen("in.in","r",stdin);
#endif // debug
    while(scanf("%d",&n)!=EOF)
    {
        init();
        for(int i=0; i<n; i++)
        {
            int ch,x;
            scanf("%d%d",&ch,&x);
            if(ch==1) enter(x);
            else
            {
                int y;
                for(int i=0; i<3; i++)
                {
                    y=output(i);
                    if(y!=x) flag[i]=0;
                }
            }
        }
        print();
    }
    return 0;
}

题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3146

题解:模拟即可。

你可能感兴趣的:(UVA 11995(p186)----I Can Guess the Data Structure!)