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 ...
随机推荐
- c/c++细节知识整理
这篇文章总结了部分c/c++琐碎的细节知识. 目录如下: (一)bool类型 知识点出处较多,无法一一列举,向原作者致敬. (一)bool类型 在c99标准以前,c语言并没有定义bool类型.如果需要 ...
- unity3d DefineManager 全局宏定义
/** * Editor Wizard for easily managing global defines in Unity * Place in Assets/Editor folder, or ...
- linux vsftpd搭建
1.yum install vsftpd; 2.关闭SELinux的方法:修改/etc/selinux/config文件中的SELINUX="" 为 disabled ,然后重启 ...
- NoClassDefFoundError: org/hibernate/annotations/common/reflection/ReflectionManager 解决方法
差一个jar包, 将hibernate-commons-annotations.jar加入到classpath中
- Java-优秀博客推荐
一. TCP/IP Socket 兰亭风雨的专栏: http://blog.csdn.net/ns_code 二. NIO 并发编程网-Java NIO系列教程:http://ifeve.com/ch ...
- tyvj3481 越狱
描述 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相信房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 输入格式 输入两个整数 ...
- LayoutComponent类,用于layout的组件类。 LayoutComponent保存的所有用于布局的数据。
LayoutComponent () 默认构造函数 更多... ~LayoutComponent () 默认的析构函数 更多... CREATE_FUNC (LayoutC ...
- Android.mk详解
Android.mk是Android提供的一种makefile文件,用来指定诸如编译生成so库名.引用的头文件目录.需要编译的.c/.cpp文件和.a静态库文件等.要掌握jni,就必须熟练掌握Andr ...
- 常用 C 头文件
ISO C 标准定义的头文件 头文件 说明 <assert.h> 验证程序断言 <complex.h> 复数算术运算支持 <ctype.h> 字符分类和映射支持 & ...
- 入门必看--JavaScript基础
JavaScript他是一种描述性语言,其实他并不难学,只要用心学,一定会学好,我相信大家在看这篇文章的时候,一定也学过HTML吧,使用JavaScript就是为了能和网页有更好的交互,下面切入主题. ...