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 is a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger elements first) or something else that you can hardly imagine!

Input There 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 x is always a positive integer not larger than 100. The input is terminated by end-of-file (EOF).

******************************************************************************************************************

Output

For 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 mentioned above.

******************************************************************************************************************

Sample Input

6

1 1     1 2     1 3     2 1     2 2     2 3

6

1 1     1 2      1 3     2 3     2 2     2 1

2

1 1     2 2

4

1 2     1 1      2 1      2 2

7

1 2     1 5      1 1      1 3     2 5      1 4       2 4

******************************************************************************************************************

Sample Output

queue

not sure

impossible

stack

priority queue

******************************************************************************************************************

题意:根据操作,判断属于那种数据结构。

思路:使用STL中对应函数,进行模拟,若矛盾则不符合。

#include<stdio.h>
#include<queue>
#include<stack>
using namespace std;
int main()
{
int n;
while (~scanf("%d", &n))
{
stack<int>st;
queue<int>qu;
priority_queue<int>pr;
int a = 1, b = 1, c = 1;
for (int i = 0; i < n; i++)
{
int x, y;
scanf("%d %d", &x, &y);
if (x == 1)//向其中加入数据
{
st.push(y);
qu.push(y);
pr.push(y);
}
else
{
if (st.empty())
{
a = b = c = 0;
continue;
}
if (a)
a = (st.top() == y);//判断顶部的数是否与y相等
if (b)
b = (qu.front() == y);
if (c)
c = (pr.top() == y);
st.pop();
qu.pop();
pr.pop();
}
}
if (a + b + c > 1)
printf("not sure\n");
else if (a)
printf("stack\n");
else if (b)
printf("queue\n");
else if (c)
printf("priority queue\n");
else
printf("impossible\n");
} return 0;
}

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

  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!(模拟)

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

  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!(ADT)

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

  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 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

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

  9. HDU 5929 Basic Data Structure(模拟 + 乱搞)题解

    题意:给定一种二进制操作nand,为 0 nand 0 = 10 nand 1 = 1 1 nand 0 = 1 1 nand 1 = 0 现在要你模拟一个队列,实现PUSH x 往队头塞入x,POP ...

随机推荐

  1. 动态改变swiper的属性

    <script> var mySwiper = new Swiper('.swiper-container',{ autoplay : 1000, autoplayDisableOnInt ...

  2. Redis实战(四)CentOS 7上Redis哨兵

    什么是哨兵 顾名思义,哨兵的作用就是对Redis的系统的运行情况的监控,它是一个独立进程.它的功能有2个: 1. 监控主数据库和从数据库是否运行正常: 2. 主数据出现故障后自动将从数据库转化为主数据 ...

  3. [php]php时间格式化

    1.将毫秒转化为时间格式 date("Y-m-d H:i:s",$millsec);

  4. Djangoform表单Ajax控制跳转

    需求: 1:在登陆页面输入账号密码后,ajax异步提交数据给后端验证. 2:验证通过后,后端指定跳转页面,并把页面封装进返回的Json数据中,由ajax控制from表单跳转到目标页面 一:登陆页面HT ...

  5. Java 8 Lambda表达式,让你的代码更简洁

    Lambda表达式是Java 8一个非常重要的新特性.它像方法一样,利用很简单的语法来定义参数列表和方法体.目前Lambda表达式已经成为高级编程语言的标配,像Python,Swift等都已经支持La ...

  6. PCA主成分分析理解

    一.理论概述 1)问题引出 先看如下几张图: 从上述图中可以看出,如果将3个图的数据点投影到x1轴上,图1的数据离散度最高,图3其次,图2最小.数据离散性越大,代表数据在所投影的维度上具有越高的区分度 ...

  7. select()函数用法二

    Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如 connect.accept.recv或recvfrom这样的阻塞程序 ...

  8. Kettle进行数据迁移(ETL)

    由于开发新的系统,需要将之前一个老的C/S应用的数据按照新的数据设计导入到新库中.此过程可能涉及到表结构不一致.大数据量(千万级,甚至上亿)等情况,包括异构数据的抽取.清洗等等工作.部分复杂的工作需要 ...

  9. Java关于网络编程回顾

    一.Java网络编程三要素:1.IP地址:是要确定发送的地址,IP地址一般分为5类. 2.端口:要确定发送的程序是哪一个,端口的范围是0--65535,其中0-1024是系统使用或保留端口 3.协议: ...

  10. ASP防XSS代码

    原作是在GitHub上,基于Node.js所写.但是..ASP的JS引擎跟V8又有些不同..于是,嗯.. <% Function AntiXSS_VbsTrim(s) AntiXSS_VbsTr ...