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 ...
随机推荐
- A better way to learn D3 js - iLearning D3.js
iLearning D3.js Basic is an iPad app to learn and code with D3. In 1.1 version, new tutorial is prov ...
- 无法连接 mysql
==================================================================================================== ...
- 【解决】Oracle服务器ip地址被占用
数据库服务器ip地址被占用,怎么破?! 服务器: 1.改服务器ip: 2.改tnsnames.ora里配置的Oracle数据库ip: 3.重启Oracle服务: 客户端: 1.改tnsnames.or ...
- HW5.9
public class Solution { public static void main(String[] args) { System.out.printf("%s\t%s\t%s\ ...
- algorithm@ Matrix fast power
一. 什么是快速幂: 快速幂顾名思义,就是快速算某个数的多少次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高.一般一个矩阵的n次方,我们会通过连乘n-1次来得到它的n次 ...
- 授权给adfs读取ad 在ad服务器上运行 - setspn 命令 -摘自网络
Because the application pool identity for the AD FS 2.0 AppPool is running as a domain user/service ...
- PC-IIS因为端口问题报错的解决方法
1.我的电脑-管理-服务和应用程序-Internet信息服务 情况:这时发现“默认 SMTP 虚拟服务器”停止 解决方法:右击启动 情况:发现网页还是打不开.2.Internet信息服务-网站- ...
- iOS消息推送机制的实现
研究了一下Apple Push Notification Service,实现的很简单,很环保.原理如下 财大气粗的苹果提供了一堆服务器,每个ios设备和这些服务器保持了一个长连接,ios版本更新提示 ...
- [一]java环境变量的配置
1.JAVA_HOME(新建):D:\jdk1.6 2.classpath(新建): .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar; 3.path(新增):% ...
- iOS从生成证书到打包上架-02(详细2016-10最新)
由于篇幅的限制,这篇接着上一篇(关于证书)写的,有需要的小伙伴可以先阅读上一篇 2.在App Store创建应用 1.回到Account,点击iTunes Connect 2.点击我的App 3.点击 ...