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

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


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. Git: 本地创建版本库用于多处同步

    问题背景 目前有一个 Android 和 一个 iOS 项目,两个项目底层使用相同的 C++ 代码.由于在开发迭代中代码时常更新,而且往往是今天 Android 部分修改一小部分,明天 iOS 部分修 ...

  2. 自己写的日志框架--linkinLog4j--日志框架的必要性

    OK,在开始研究Log4j的源码之前,我们先来自己模拟一个日志工具,名字就叫linkinlog4j好了. 在软件开发过程中,出现bug总是在所难免:事实上,以我个人经验,即使在实际开发阶段,fix b ...

  3. java常用类--系统相关

    java提供了System类和Runtime类来与程序的运行平台进行交互. System类 System类代表java程序的运行平台,程序不能创建这个类的对象,System类提供了一些类field和方 ...

  4. base64_encode与base64_decode

    base64_encode 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! base64_encode() returns 使用 base64 对 data 进行编码. ...

  5. 错误:You can't specify target table 'xxx' for update in FROM clause的解决

    问题: 今天在MySQL数据库删除重复数据的时候遇到了一个问题.如下脚本: DELETE FROM tempA WHERE tid IN ( SELECT MAX(tid) AS tid FROM t ...

  6. MYSQL 5.7 修改密码、登录问题

    mysql5.7 关于密码问题 报错: ERROR 1862 (HY000): Your password has expired. To log in you must change it usin ...

  7. openvpn的搭建

    openvpn搭建 原创不易,转载请注明 openvpn简介 1.1 openvpn原理 OpenVpn的技术核心是虚拟网卡,其次是SSL协议实现 虚拟网卡是使用网络底层编程技术实现的一个驱动软件,安 ...

  8. 【转】awk 数组用法【精华贴】

    文本处理的工作中,awk的数组是必不可少的工具,在这里,同样以总结经验和教训的方式和大家分享下我的一些学习心得,如有错误的地方,请大家指正和补充. awk的数组,一种关联数组(Associative ...

  9. Win10下通过IIS调试ASP程序遇到的问题和解决方案

    最近维护了以前别人的写的一个ASP的系统,记录一下调试过程中的问题和解决方案. 环境篇 万维网发布服务(W3SVC)已经停止 问题: 万维网发布服务(W3SVC)已经停止.除非万维网发布服务(W3SV ...

  10. 新建play项目eclipsify后导入eclipse后无法debug调试

    Error occurred during initialization of VMagent library failed to init: jdwpERROR: Cannot load this ...