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

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


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. docker搭建私服

    拉registry镜像 假设在192.168.100.17服务器上搭建私服 docker pull registry docker run -d -v /data/docker/registry:/v ...

  2. java.lang.Thread

    package java.lang; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java. ...

  3. java IO(四):键盘录入

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  4. Android Camera2采集摄像头原始数据并手动预览

    Android Camera2采集摄像头原始数据并手动预览 最近研究了一下android摄像头开发相关的技术,也看了Google提供的Camera2Basic调用示例,以及网上一部分代码,但都是在Te ...

  5. Git 生成 SSH 公钥

    2018-01-05 11:24:04 许多 Git 服务器都使用 SSH 公钥进行认证. 为了向 Git 服务器提供 SSH 公钥,如果某系统用户尚未拥有密钥,必须事先为其生成一份. 这个过程在所有 ...

  6. notepad++代码输出电话号

    在网上看到一个程序员找合租的消息,希望找一个程序员合租,所以电话号码以代码的形式输出,闲来没事打出来玩玩.首先新建一个test.java文件,并敲入下面代码: 1 public class test{ ...

  7. WindowXp-Windows7-Windows运行命令(转)

    Win7里面按 Win+R 呼出运行界面,一下是它的一些常用命令: 1.cleanmgr: 打开磁盘清理工具 2.compmgmt.msc: 计算机管理 3.conf: 启动系统配置实用程序 4.ch ...

  8. ABP框架源码学习之修改默认数据库表前缀或表名称

    ABP框架源码学习之修改默认数据库表前缀或表名称 1,源码 namespace Abp.Zero.EntityFramework { /// <summary> /// Extension ...

  9. Yii 2.0.3 Advanced版控制器不能包含大写字母的Bug

    Yii 2.0.3 Advanced版控制器不能包含大写字母的Bug,我是直接下载Archive文件安装的,非Composer方式安装 Yii 框架之前是支持在Url中包含大写字母的 最新的Yii 2 ...

  10. The Go Programming Language. Notes.

    Contents Tutorial Hello, World Command-Line Arguments Finding Duplicate Lines A Web Server Loose End ...