UVA - 11995 I Can Guess the Data Structure! 猜猜数据结构(STL模拟)

来源:互联网 发布:网络格斗游戏排行 编辑:程序博客网 时间:2024/05/16 06:10

大体题意:

有两种操作   1  x 把 x 放到集合里。

2 x 在x中取出的第一个元素是x

问这个集合是优先队列还是普通队列 还是栈。还是不能确定 还是不可能!

思路:

直接拿着三个东西模拟一下即可!

比较简单 具体不多说了,详细见代码:

#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<stack>using namespace std;priority_queue<int,vector<int>,less<int> >q;stack<int>s;queue<int>q2;int main(){    int n;    while(scanf("%d",&n) == 1){        while(!q.empty())q.pop();        while(!q2.empty())q2.pop();        while(!s.empty())s.pop();        int x,y,z;        x = y = z = 1;        for (int i = 0; i < n; ++i){            int u,v;            scanf("%d %d",&u,&v);            if (u == 1){                q.push(v);                q2.push(v);                s.push(v);                continue;            }            if (x){                if (q.empty() || q.top() != v)x = 0;                else q.pop();            }            if (y){                if (q2.empty() || q2.front() != v)y = 0;                else q2.pop();            }            if (z){                if (s.empty() || s.top() != v)z = 0;                else s.pop();            }        }        if (x + y + z > 1)printf("not sure\n");        else if (x + y + z == 0)printf("impossible\n");        else if (x)printf("priority queue\n");        else if (y)printf("queue\n");        else printf("stack\n");    }    return 0;}


0 0
原创粉丝点击