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
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
Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!
数据结构练习。
这道题的题目是“猜猜数据结构”,题意就是给你一些输入输出数据,让你根据这些数据判断是什么数据结构。要猜的数据结构只有三种,栈(stack)、队列(queue)、优先队列(priority_queue)。输出有5种情况,前三种分别是确定了一种数据结构,第四种是三种数据结构都不符合,第五种是有2种或2种以上符合。
思路就是在程序中定义这三种数据结构,根据输入数据,产生各自的输出结果,分别与给定的输出输出对比。如果与测试数据不同,则这种数据结构不可能。最后记录下符合的数据结构的个数,分情况判断输出即可。
代码:
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
int main()
{
int i,n;
while(cin>>n){
queue <int> q;
priority_queue <int> pq;
stack <int> s;
bool f[] = {};
for(i=;i<=n;i++){
int a,b;
cin>>a>>b;
if(a==){ //入
q.push(b);
pq.push(b);
s.push(b);
}
else{ //出
//依次对比
if(!f[] && !q.empty()){
int x = q.front();
q.pop();
if(x!=b) f[]=true;
}
else f[]=true; if(!f[] && !pq.empty()){
int x = pq.top();
pq.pop();
if(x!=b) f[]=true;
}
else f[]=true; if(!f[] && !s.empty()){
int x = s.top();
s.pop();
if(x!=b) f[]=true;
}
else f[]=true;
}
}
//查找有几个符合输出
int num=;
for(i=;i<;i++)
if(!f[i])
num++;
if(num==)
cout<<"impossible"<<endl;
else if(num==){
if(!f[])
cout<<"queue"<<endl;
else if(!f[])
cout<<"priority queue"<<endl;
else if(!f[])
cout<<"stack"<<endl;
}
else
cout<<"not sure"<<endl;
}
return ;
}
Freecode : www.cnblogs.com/yym2013
UVa 11995:I Can Guess the Data Structure!(数据结构练习)的更多相关文章
- [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!(ADT)
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 模拟
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 ...
- UVA - 11995 I Can Guess the Data Structure!(模拟)
思路:分别定义栈,队列,优先队列(数值大的优先级越高).每次放入的时候,就往分别向三个数据结构中加入这个数:每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结 ...
- 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 ...
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
随机推荐
- subplot的应用
import matplotlib.pyplot as Plot Plot.subplot(3, 4, (1, 7)) Plot.subplot(1, 4, 4) Plot.subplot(3, 4, ...
- Java中数据类型及其之间的转换
Java中数据类型及其之间的转换 基本的数据类型 基本类型有以下四种:1)int长度数据类型有:byte(8bits).short(16bits).int(32bits).long(64bits).2 ...
- C++_Eigen函数库用法笔记——The Array class and Coefficient-wise operations
The advantages of Array Addition and subtraction Array multiplication abs() & sqrt() Converting ...
- Oracle 11g r2 x64 中文乱码解决方案
1.检查服务器编码: 执行SQL语法: select * from v$nls_parameters; 2.设置本地客户端编码: 进入 我的电脑,属性,高级,环境变量,添加2项:LANG=zh_CN. ...
- Gradle用户指南(2)-基本命令
1.定义任务 使用task 关键字来定义gradle 任务 例:创建一个 build.gradle 命名的文件: build.gradle task hello { doLast { println ...
- DEDECMS 后台登录空白怎么办 后台无法登陆
刚安装完dedecms,兴致冲冲的准备进后台,输入完用户名和密码后,页面 中显示一片空白. 立马到网上搜搜,发现大家各抒己见,但是都没有解决问题. 不过,下面的这个方法是可以的.马上记录下来,以备其他 ...
- C语言课程2——我们交流的工具:Coding.net
各位同学,大家好,在我们本学期既有老师的课堂授课,同样也有我与你们在线的辅导:那么问题来了,我与你们之间是通过何种方式进行交流,比如你的代码我怎么修改,怎样看到修改了哪些地方,我对你们的代码怎样批注, ...
- hiho一下 第九十六周 数论五·欧拉函数
题目1 : 数论五·欧拉函数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho有时候会用密码写信来互相联系,他们用了一个很大的数当做密钥.小Hi和小Ho约定 ...
- 数据库多张表导出到excel
数据库多张表导出到excel public static void export() throws Exception{ //声明需要导出的数据库 String dbName = "hdcl ...
- 细微之处:比较两种CSS清除浮动的兼容
http://www.cnblogs.com/bienfantaisie/archive/2011/05/27/2059597.html 清除浮动是连续浮动元素之后的必备工作,在工作中我做到需要清除浮 ...