I Can Guess the Data Structure!

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). The size of input file does not exceed 1MB.

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

Output for the Sample Input

queue
not sure
impossible
stack
priority queue 题目大意:有一个类似“包包”的数据结构,支持两种操作。1x:把元素x放进包包;2:从包包中拿出一个元素。给出一系列的操作以及返回值,你的任务是猜猜这个“包包”到底是什么。它可能是一个栈,队列,优先队列或者其他什么奇怪的东西。 分析:只需要依次判断输入是否可能是栈,队列或优先队列,然后中合起来即可。注意到题目中说的“无错的返回”,因此在执行pop操作的时候要调用一下empty(),否则可能会异常退出。 代码如下:
 #include<cstdio>
#include<queue>
#include<stack>
#include<cstdlib>
using namespace std; const int maxn = + ;
int n, t[maxn], v[maxn]; int check_stack() {
stack<int> s;
for(int i = ; i < n; i++) {
if(t[i] == ) {
if(s.empty()) return ;
int x = s.top(); s.pop();
if(x != v[i]) return ;
}
else s.push(v[i]);
}
return ;
} int check_queue() {
queue<int> s;
for(int i = ; i < n; i++) {
if(t[i] == ) {
if(s.empty()) return ;
int x = s.front(); s.pop();
if(x != v[i]) return ;
}
else s.push(v[i]);
}
return ;
} int check_pq() {
priority_queue<int> s;
for(int i = ; i < n; i++) {
if(t[i] == ) {
if(s.empty()) return ;
int x = s.top(); s.pop();
if(x != v[i]) return ;
}
else s.push(v[i]);
}
return ;
} int main() {
while(scanf("%d", &n) == ) {
for(int i = ; i < n; i++) scanf("%d%d", &t[i], &v[i]);
int s = check_stack();
int q = check_queue();
int pq = check_pq();
if(!s && !q && !pq) printf("impossible\n");
else if(s && !q && !pq) printf("stack\n");
else if(!s && q && !pq) printf("queue\n");
else if(!s && !q && pq) printf("priority queue\n");
else printf("not sure\n");
}
return ;
}
 

UVA 11995 I Can Guess the Data Structure!(ADT)的更多相关文章

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

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

  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! [STL应用]

    11995 - I Can Guess the Data Structure! Time limit: 1.000 seconds Problem I I Can Guess the Data Str ...

  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. 高级数据结构学习笔记 / Data Structure(updating)

    树状数组   查询操作:O(logn) 修改操作:O(logn) #define lowbit(x) (x & -x) int tr[N]; // 树状数组 // 添加c个大小为x的数值 vo ...

  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. 搭建Windows Azure开发环境-Azure虚拟机

    概念 这一节是关于让设置了Azure的虚拟机,它包括的Visual Studio 2013 RC旗舰版和SQL Server 2012 Express的 . 在此动手实验中,您将探索建立使用Windo ...

  2. java操作pdf添加页眉条码添加水印图片

    添加条码页眉以及图片水印 1. 引入jar包     1. itext-4.2.1.jar     2. itext-asian-5.2.0.jar     3. jbarcode-0.2.8.jar ...

  3. zoj Simple Equation 数论

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5176 AX+BY = XY  => (X-B)*(Y-A)= ...

  4. ios 免书籍入门站点

    http://www.raywenderlich.com/tutorials http://www.raywenderlich.com/ios-tutorials http://web.stanfor ...

  5. CodeForces 540E - Gerald and Giant Chess(数论)

    给一个棋盘,需要从左上角走到右下角,有部分点不能走,求一共有多少种走法. 首先要知道从一个点A到另一个点B在没有障碍下有多少种走法.保证A在B的左上方,如图 一共需要走(X+Y)步(图中△x,△y), ...

  6. yii 获取系统级请求参数的常用方法

    1.GET/POST 1.1.获取GET/POST过来的数据 Yii::app()->request->getParam('id'); 1.2.判断数据提交方式 Yii::app()-&g ...

  7. nyoj 540 奇怪的排序

    奇怪的排序 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 最近,Dr. Kong 新设计一个机器人Bill.这台机器人很聪明,会做许多事情.惟独对自然数的理解与人类 ...

  8. FFT(快速傅立叶算法 for java)

    package com.test.test2; public class FFT {     public static final int FFT_N_LOG = 10; // FFT_N_LOG ...

  9. Fast特征检测

    一.Fast算法 1.基本原理 Fast特征点检测feature2D原理是在圆周上按顺时针方向从1到16的顺序对圆周像素点进行编号.如果在圆周上有N个连续的像素的亮度都比圆心像素的亮度Ip加上阈值t还 ...

  10. drop table big_table 调试

    (gdb) thread apply all bt Thread (Thread )): # # /storage/innobase/os/os0sync.cc: # ) at /usr/src/my ...