判断STL的三种类型——UVA11995 - I Can Guess the Data Structure!

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=497&page=show_problem&problem=3146

STL的使用,同时注意出栈出队列是的 empty判断

判断STL的三种类型——UVA11995 - I Can Guess the Data Structure! View Code
#include<iostream>

#include<queue>

#include<stack>

#include<stdio.h>

using namespace std;



struct data 

{

    int v;

    int k;



    friend bool operator <(data a,data b){

        return a.v<b.v;

    }

    

}s[1099];

int n;



int ceque()

{

    int i;

    queue<int>qq;

    for(i=1;i<=n;i++){

        if(s[i].k==1)qq.push(s[i].v);

        if(s[i].k==2){

            if(qq.empty()==1)return 0;

            if(qq.front()==s[i].v){

                qq.pop();continue;

            }

            else return 0;

        }

    }



    return 1;

}



int cesta()

{

    int i;

    stack<int>sta;

    for(i=1;i<=n;i++){

        if(s[i].k==1)sta.push(s[i].v);

        if(s[i].k==2){

            if(sta.empty()==1)return 0;

            if(sta.top()==s[i].v){

                sta.pop();continue;

            }

            else return 0;

        }

    }



    return 1;

}



int cepriorityq()

{

    priority_queue<data>qq;

    int i;

    for(i=1;i<=n;i++){

        if(s[i].k==1)qq.push(s[i]);

        if(s[i].k==2){

            if(qq.empty()==1)return 0;

            if(qq.top().v==s[i].v){

                qq.pop();continue;

            }

            else return 0;

        }

    }



    return 1;

}



int main()

{



    while(scanf("%d",&n)!=EOF){

        int i;

        for(i=1;i<=n;i++){

            scanf("%d%d",&s[i].k,&s[i].v);

        }



        int yq=ceque(),ys=cesta(),ypq=cepriorityq();

        if((yq+ys+ypq)>=2)printf("not sure\n");

        else if(yq)printf("queue\n");

        else if(ys)printf("stack\n");

        else if(ypq)printf("priority queue\n");

        else printf("impossible\n");

    }



    return 0;

}

 

你可能感兴趣的:(struct)