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

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


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. nginx添加编译lua模块

    一 .安装LuaJit 1.下载LuaJit # wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz 2.编译安装 # tar xzvf LuaJI ...

  2. centos7创建新用户

    创建新用户 创建一个叫xiaoming的用户: [root@192 ~]# adduser xiaoming 为这个用户初始化密码,linux会判断密码复杂度,不过可以强行忽略: [root@192 ...

  3. Servlet--HttpServletRequest一些不常用的方法

    我们在使用Servlet和表单进行交互的时候,不管是传参和接参经常要写一些路径.关于具体的Servlet的传参和接参我后面会有详细的整理,这里先整理一下不怎么常用的到一些HttpServletRequ ...

  4. 在 Tomcat 上配置虚拟主机

    .Tomcat 服务器的server.xml文件   (1)Tomcat 组件 Tomcat服务器是由一系列可配置的组件构成,其中核心组件是 Catalina Servlet 容器,它是所有其他 To ...

  5. linkin大话数据结构--Collection和Iterator

    linkin大话数据结构--Collection和Iterator Java 集合就像一种容器,可以把多个对象的引用放入容器中.Java 集合类可以用于存储数量不等的多个对象,还可用于保存具有映射关系 ...

  6. 2018-01-28-TF源码做版本兼容的一个粗暴方法

    layout: post title: 2018-01-28-TF源码做版本兼容的一个粗暴方法 key: 20180128 tags: IT AI TF modify_date: 2018-01-28 ...

  7. php之快速排序

     <?phpfunction shell_sort(array $arr){        $right=$left = array();    $Rights=$Lefts = array() ...

  8. 跨进程的mutex

    1.操作系统分为ring0(内核层)和ring3(应用层)两层. ring0层出错会蓝屏,ring3层出错程序就会挂了. event和mutex创建他的指针是应用层,但是它的内部是ring0层,rin ...

  9. 在VMware中安装ubuntu

    如果想在自己设置它的属性,比如时区,语言支持之类的,在开机时按[enter], 否则就是默认的设置了,英文语言,而且还不好调.

  10. Electron 打包Mac安装包代码签名问题解决方案Could not get code signature for running application

    最近一直在做electron应用的打包,集成mac版本的自动更新时出现了问题. Error: Could not get code signature for running application ...