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
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)的更多相关文章
- UVA - 11995 I Can Guess the Data Structure!(模拟)
思路:分别定义栈,队列,优先队列(数值大的优先级越高).每次放入的时候,就往分别向三个数据结构中加入这个数:每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结 ...
- 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 ...
- [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 ...
- 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. ...
- STL UVA 11995 I Can Guess the Data Structure!
题目传送门 题意:训练指南P186 分析:主要为了熟悉STL中的stack,queue,priority_queue,尤其是优先队列从小到大的写法 #include <bits/stdc++.h ...
- UVa 11995 I Can Guess the Data Structure!
做道水题凑凑题量,=_=||. 直接用STL里的queue.stack 和 priority_queue模拟就好了,看看取出的元素是否和输入中的相等,注意在此之前要判断一下是否非空. #include ...
- uva 11995 I Can Guess the Data Structure stack,queue,priority_queue
题意:给你n个操做,判断是那种数据结构. #include<iostream> #include<cstdio> #include<cstdlib> #includ ...
- 高级数据结构学习笔记 / Data Structure(updating)
树状数组 查询操作:O(logn) 修改操作:O(logn) #define lowbit(x) (x & -x) int tr[N]; // 树状数组 // 添加c个大小为x的数值 vo ...
- 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 ...
随机推荐
- 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程06:技能播放的逻辑关系》
6.技能播放的逻辑关系 技能播放概述: 当完成对技能输入与检测之后,程序就该对输入在缓存器中的按键操作与程序读取的技能表信息进行匹配,根据匹配结果播放相应的连招技能. 技能播放原理: 按键缓存器中内容 ...
- linux Grant 添加 MySql 用户
Grant 添加 MySql 用户 2009-04-03 14:40 我安装的版本: mysql> select version();+------------+| version() |+ ...
- Floyd-Warshall算法的理解
Floyd算法可以求图内任意两点之间的最短路径,三重循环搞定,虽然暴力,但是属于算法当中最难的动态规划的一种,很有必要理解. 花了一晚上和半个下午专门看这个,才看个一知半解,智商被碾压没办法. 我一直 ...
- POJ-1981 Circle and Points 单位圆覆盖
题目链接:http://poj.org/problem?id=1981 容易想到直接枚举两个点,然后确定一个圆来枚举,算法复杂度O(n^3). 这题还有O(n^2*lg n)的算法.将每个点扩展为单位 ...
- linux下mysql的表名问题
最近从win转移到了linux,在本机跑好的程序但在linux下一个SQL语句报了错误,发现是表名未找到,错误原因是在linux下mysql的表名是严格区分大小写的.. MYSQL在LINUX下数据库 ...
- SignalR Supported Platforms -摘自网络
SignalR is supported under a variety of server and client configurations. In addition, each transpor ...
- 大型机汇编(mainframe assembler/HLASM)之COBOL解惑
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. ENVIRONMENT DIVISION. ...
- ThinkPHP3.1新特性:Action参数绑定
Action参数绑定功能提供了URL变量和操作方法的参数绑定支持,这一功能可以使得你的操作方法定义和参数获取更加清晰,也便于跨模块调用操作方法了.这一新特性对以往的操作方法使用没有任何影响,你也可以用 ...
- PHP工具下载地址
Zend Debugger下载地址:http://www.zend.com/en/products/studio/downloads 需要先注册一下,然后才能进行下载.
- Lucene教程具体解释
(建立索引)] )中生成的索引文件的存放地址.详细步骤简单介绍例如以下: 1.创建Directory对象,索引目录 2.创建IndexSearch对象,建立查询(參数是Directory对象) 3.创 ...