There is a bag-like data structure, supporting two operations:1 x Throw an element x into the bag.2 Take out an element from the bag.Given a sequence of operations with return values, you’re going to guess the data structure. It isa stack (Last-In, First-Out),
a queue (First-In, First-Out), a priority-queue (Always take out largerelements first) or something else that you can hardly imagine!InputThere are several test cases. Each test case begins with a line containing a single integer n (1 ≤ n ≤1000). Each of the
next n lines is either a type-1 command, or an integer 2 followed by an integer x.That means after executing a type-2 command, we get an element x without error. The value of xis always a positive integer not larger than 100. The input is terminated by end-of-file
(EOF).OutputFor each test case, output one of the following:stack It’s definitely a stack.queue It’s definitely a queue.priority queue It’s definitely a priority queue.impossible It can’t be a stack, a queue or a priority queue.not sure It can be more than
one of the three data structures mentionedabove.Sample Input61 11 21 32 12 22 361 11 21 32 32 22 121 12 241 21 12 12 271 21 51 11 32 51 42 4Sample Outputqueuenot sureimpossiblestackpriority queue

题解:分别定义 stack、queue、priority_queue 判断这个操作序列是不是符合上述结构。

AC代码为:

#include<stdio.h>  

#include<stack>  

#include<queue>  

using namespace std;  

int main()  

{  

    int n, i, x, y, f[4];  

    while(~scanf("%d",&n))  

    {  

        stack<int> s;  

        queue<int> q;  

        priority_queue<int, vector<int>, less<int> > pq;  

        for(i=0;i<3;i++) 
f[i]=1;  

        for(i=0;i<n;i++)  

        {  

            scanf("%d%d",&x,&y);  

            if(x == 1)  

            {  

                s.push(y);  

                q.push(y);  

                pq.push(y);  

            }  

            else  

            {  

                if(!s.empty())  

                {  

                    if(s.top() != y)  

                        f[0] = 0;  

                    s.pop();  

                }  

                else  

                    f[0] = 0;  

  

                if(!q.empty())  

                {  

                    if(q.front() != y)  

                        f[1] = 0;  

                    q.pop();  

                }  

                else  

                    f[1] = 0;  

  

                if(!pq.empty())  

                {  

                    if(pq.top() != y)  

                        f[2] = 0;  

                    pq.pop();  

                }  

                else  

                    f[2] = 0;  

            }  

        }  

        int num = 0;  

        for(i = 0; i < 3; i++)  

            if(f[i] == 1)  

                num++;  

        if(num == 0)  

            printf("impossible\n");  

        else if(num > 1)  

            printf("not sure\n");  

        else  

        {  

            if(f[0] == 1)  

                printf("stack\n");  

            else if(f[1] == 1)  

                printf("queue\n");  

            else  

                printf("priority queue\n");  

        }  

    }  

    return 0;  

}

UVA-11995的更多相关文章

  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. STL UVA 11995 I Can Guess the Data Structure!

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

  3. 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 ...

  4. UVa 11995 I Can Guess the Data Structure!

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

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

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

  6. 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 ...

  7. UVA - 11995 I Can Guess the Data Structure!(模拟)

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

  8. 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. ...

  9. UVA - 11995 模拟

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #i ...

  10. UVA 11995 STL 使用

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

随机推荐

  1. ios input输入不了

    在项目中遇到了一个问题就是input输入框在安卓可以输入,而在ios输入不了 经过百度,调试发现,在ios中input默认是有user-select: none;属性把input输入框禁用了,将其删除 ...

  2. 关于MXNet

    关于人工智能,机器学习,深度学习 三者关系:从宏观到微观. 机器学习是人工智能的一部分,深度学习是机器学习的一部分. 基础:大数据. 关于深度学习 深度学习基于神经网络, 关于神经网络:通过叠加网络层 ...

  3. nyoj 803-A/B Problem

    803-A/B Problem 内存限制:64MB 时间限制:1000ms 特判: No 通过数:2 提交数:4 难度:3 题目描述: 做了A+B Problem,A/B Problem不是什么问题了 ...

  4. Python3.7.1学习(三)求两个list的差集、并集与交集

    在python3.7.1对列表的处理中,会经常使用到Python求两个list的差集.交集与并集的方法. 下面就以实例形式对此加以分析. # 求两个list的差集.并集与交集# 一.两个list差集# ...

  5. 【NServiceBus】什么是Saga,Saga能做什么

    前言           Saga单词翻译过来是指尤指古代挪威或冰岛讲述冒险经历和英雄业绩的长篇故事,对,这里强调长篇故事.许多系统都存在长时间运行的业务流程,NServiceBus使用基于事件驱动的 ...

  6. 扛把子组2018092609-2 选题 Scrum立会报告+燃尽图 04

    此作业的要求参见[https://edu.cnblogs.com/campus/nenu/2019fall/homework/8682] 一.小组情况组长:迟俊文组员:宋晓丽 梁梦瑶 韩昊 刘信鹏队名 ...

  7. nmap中的详细命令

    nmap全部参数详解-A 综合性扫描端口:80http 443https 53dns 25smtp 22ssh 23telnet20.21ftp 110pop3 119nntp 143imap 179 ...

  8. Python3学习-基础

    1.直接运行.py文件 在Windows上是不行的,但是在Mac和Linux上是可以的,方法是在.py文件的第一行加上一个特殊的注释: #!/usr/bin/env python3 print('he ...

  9. Glibc编译报错:*** LD_LIBRARY_PATH shouldn't contain the current directory when*** building glibc. Please change the environment variable

    执行glibc编译出错如下图 [root@localhost tmpdir]# ../configure --prefix=/usr/loacl/glibc2.9 --disable-profile ...

  10. Netty-主从Reactor多线程模式的源码实现

    Netty--主从Reactor多线程模式的源码实现 总览 EventLoopGroup到底是什么? EventLoopGroup是一个存储EventLoop的容器,同时他应该具备线程池的功能. gr ...