思路:分别定义栈,队列,优先队列(数值大的优先级越高)。每次放入的时候,就往分别向三个数据结构中加入这个数;每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结构,可以用三个标志变量来标志可能的数据结构。

注意:可能会出现数据结构为空的情况,要先判断非空才能取元素。


AC代码

#include <stdio.h>
#include <stack>
#include <queue>
using namespace std;

//分别定义栈,队列,优先队列,然后模拟
stack<int> sta;
queue<int> que;
priority_queue<int> p_que;

void init() {
    while(!sta.empty()) sta.pop();
    while(!que.empty()) que.pop();
    while(!p_que.empty()) p_que.pop();
}

int main() {
    int n;
    while(scanf("%d", &n) == 1) {
        init();
        int tag1 = 1, tag2 = 1, tag3 = 1;
        for(int i = 0; i < n; ++i) {
            int tag, num;
            scanf("%d %d", &tag, &num);
            if (tag == 1) {
                sta.push(num);
                que.push(num);
                p_que.push(num);
            } else if(tag == 2) {
                if (sta.empty() || sta.top() != num) {
                    tag1 = 0;
                } else sta.pop();

                if (que.empty() || que.front() != num) {
                    tag2 = 0;
                } else que.pop();

                if (p_que.empty() || p_que.top() != num) {
                    tag3 = 0;
                } else p_que.pop();
            }
        }

        int res = tag1 + tag2 + tag3;
        if (res == 0) {
            printf("impossible\n");
        } else if(res >= 2) {
            printf("not sure\n");
        } else {
            if (tag1) {
                printf("stack\n");
            } else if (tag2) {
                printf("queue\n");
            } else printf("priority queue\n");
        }
    }
    return 0;
}

如有不当之处欢迎指出!

UVA - 11995 I Can Guess the Data Structure!(模拟)的更多相关文章

  1. [UVA] 11995 - I Can Guess the Data Structure! [STL应用]

    11995 - I Can Guess the Data Structure! Time limit: 1.000 seconds Problem I I Can Guess the Data Str ...

  2. UVa 11995:I Can Guess the Data Structure!(数据结构练习)

    I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...

  3. UVA 11995 I Can Guess the Data Structure!(ADT)

    I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...

  4. UVA - 11995 - I Can Guess the Data Structure! STL 模拟

    There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...

  5. STL UVA 11995 I Can Guess the Data Structure!

    题目传送门 题意:训练指南P186 分析:主要为了熟悉STL中的stack,queue,priority_queue,尤其是优先队列从小到大的写法 #include <bits/stdc++.h ...

  6. UVa 11995 I Can Guess the Data Structure!

    做道水题凑凑题量,=_=||. 直接用STL里的queue.stack 和 priority_queue模拟就好了,看看取出的元素是否和输入中的相等,注意在此之前要判断一下是否非空. #include ...

  7. uva 11995 I Can Guess the Data Structure stack,queue,priority_queue

    题意:给你n个操做,判断是那种数据结构. #include<iostream> #include<cstdio> #include<cstdlib> #includ ...

  8. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  9. uva-11995 - I Can Guess the Data Structure!(栈,优先队列,队列,水题)

    11995 - I Can Guess the Data Structure! There is a bag-like data structure, supporting two operation ...

随机推荐

  1. js swipeDelete 滑动删除

    先上图看看整体实际效果 看看用法 与参数 默认参数 var defaults = { distance:80, //滑动距离 units:'px', //默认单位 touchStart:functio ...

  2. AMS的适用场景

    AMS适用于网络音视频应用的各种场合,可以独立作为直播点播平台应用,也可以嵌入到用户的各种应用平台中,为客户提供音视频核心支撑,不同于其它提供云服务租给客户使用的产品,AMS是一套安装在企业内部服务器 ...

  3. Log4j源码解析--LoggerRepository和Configurator解析

    本文转自上善若水的博客,原文出处:http://www.blogjava.net/DLevin/archive/2012/07/10/382678.html.感谢作者的无私分享. LoggerRepo ...

  4. Linux几个小杂碎点(更新中)

    1 BIOS时间和系统时间问题 安装完CentOS后,系统时间是CST时间,而BIOS时间是UTC时间,因此系统时间会比BIOS时间快8个小时.如果您设置BIOS自动开机的话,就会总是差个8小时.需要 ...

  5. mybatis支持oracle批量插入

    问题:mysql使用mybatis批量插入时,通过foreach标签,将每条记录按照逗号","连接即可. 但是,oracle不支持. oracle支持如下写法: <inser ...

  6. python selenium+phantomjs alert()弹窗报错

    问题:用selenium+phantomjs 模拟登陆,网页用JavaScript的alert("登陆成功")弹出框,但是用switch_to_alert().accept()报错 ...

  7. HTML5中的Web Notification桌面通知

    大家在做一些浏览器端的聊天功能的时候,或者在一些网站跟在线客服咨询的时候,会看到一些消息通知的提示,常见的有浏览器标签页的闪烁和屏幕右侧的消息通知.本篇博客就在这里简单的介绍一下如何实现这样的功能. ...

  8. 纯静态界面中(html)中通过js调用dll中的方法从数据库中读取数据

    最近接到一个离职员工的任务,一个搭好框架的ERP系统,看了两天,说一下看到的东西,整个项目目录中我没发现一个.aspx后缀的文件,全是静态HTML文件,之后发现他用的jquery简直看的头疼,不过大概 ...

  9. 洛谷 [P2483] [模板] k短路

    人生中的第一道黑题... 其实就是k短路模板 #include <iostream> #include <cstdio> #include <cstring> #i ...

  10. 洛谷 [P1282] 多米诺骨牌

    这道题是一道背包问题,考虑一个背包, 显然如果我们直接设dp[i]表示前i个使差值最小所需的最少翻转次数,是具有后效性的. 所以我们将直接求最值,改为求某个值是否可行,这种求最值转变为求可行性的思想是 ...