UVA 11995 I Can Guess the Data Structure!

来源:互联网 发布:怎样写淘宝直通车标题 编辑:程序博客网 时间:2024/06/07 23:18

题目大意: 

直接见白书。



解题思路:

简单的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;}


0 0