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. qualcomm platform camera porting

    转载自http://www.cnblogs.com/thjfk/p/4086001.html camera基本代码架构 Camera原理:外部光线穿过lens后,经过color filter滤波后照射 ...

  2. Word恢复文本转换器-修复损坏的WORD文件

    第一步:找任意一个未损坏的文件打开word,新建的或者是已有的好的word文档,在文档的工具-选项-常规中,选中“打开时确认转换”复选框,并按确定. 第二步:点击word软件左上角的 文件-打开,找到 ...

  3. Dynamics CRM 2011 2013-(An error occurred while opening mailbox xxx@xx.com Microsoft.Crm.Tools.Email.Providers.)

    An error occurred while opening mailbox  Microsoft.Crm.Tools.Email.Providers. Whenever I check how C ...

  4. C#- 压缩和解压缩的研究 .

    用了第二种方法,感觉很不错,其他都没用过了.摘录下来,做一个备忘. 最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种: 1.利用.net自带的压缩和解压缩方法GZip ...

  5. 如何用Windbg找到被catch住的C++的异常

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何用Windbg找到被catch住的C++的异常.

  6. 动态调用DLL函数有时正常,有时报Access violation的异常

    动态调用DLL函数有时正常,有时报Access violation的异常 typedef int (add *)(int a,int b); void test() {     hInst=LoadL ...

  7. [IOS地图开发系类]2、位置解码CLGeocoder

      接第一步的操作,获取到地址信息经纬度后,我们可以对其进行解码,解码采用的CLGeocoder这个类,使用方式如下: 1.在ViewControlelr.m文件中声明一个CLGeocoder的属性, ...

  8. 【转】C++的面象对象总结

    转自:http://www.cnblogs.com/icemoon1987/archive/2012/10/01/2709572.html 1. 面向对象:对象.类.继承   2. 构造函数: 类的数 ...

  9. SEAndroid安全机制对Binder IPC的保护分析

    在SEAndroid安全机制中,除了文件和属性,还有Binder IPC须要保护.Binder IPC是Android系统的灵魂,使用得相当广泛又频繁.比如,应用程序都是Binder IPC请求訪问系 ...

  10. AndroidStaggeredGrid

    https://github.com/etsy/AndroidStaggeredGrid