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 ...
随机推荐
- 第一部分 mongodb 基础篇
什么是NoSQL认识MongoDBMongDB的下载与安装MongoDB的体系结构常用命令(基本的增删改查)客户端GUI工具集合 一: 什么是NoSql1 NoSQL简介NoSQL是Not Only ...
- MAC Java 开发环境配置
JDK Oracle官网 -> Download -> Java for Developers -> Java SE Downloads -> Java Platform (J ...
- mysql 外键(FOREIGN KEY)
最近有开始做一个实验室管理系统,因为分了几个表进行存储·所以要维护表间的关联··研究了一下MySQL的外键. (1)只有InnoDB类型的表才可以使用外键,mysql默认是MyISAM,这种类型不支持 ...
- word表格断行的问题
word一个表格如果某一行的 内容 太多,就会自动跑到下一页去了 解决方法是: 在表格上点右键-> 属性 -> "行" -> 去掉"设置行高" ...
- 如何调试R程序(转载)
R语言的调试重要性不言而喻,这段时间准备改进一个R的包,但由于接触R时间不长,中间的很多东西不懂,需要重新打包调试,以对里面的很多程序有深入了解,下面从几个方面分享一下我的收获. 1.准备工作 a)R ...
- 繁华模拟赛 Vincent的城堡
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- 完美串(区间dp)
完美串 Description 爱美之心人皆有之,GG也不例外.所以GG他对于完美串有一种热衷的爱.在GG眼中完美串是一个具有无比魅力的01子串.这个子串有之其魅力之处,对它取反后水平翻转,它又和它原 ...
- cocos基础教程(11)事件分发机制
cocos3.0的事件分发机制: 创建一个事件监听器-用来实现各种触发后的逻辑. 事件监听器添加到事件分发器_eventDispatcher,所有事件监听器有这个分发器统一管理. 事件监听器有以下几种 ...
- Toast工具类,Android中不用再每次都写烦人的Toast了
package com.zhanggeng.contact.tools; /** * Toasttool can make you use Toast more easy ; * * @author ...
- codeforces 258div2C Predict Outcome of the Game
题目链接:http://codeforces.com/contest/451/problem/C 解题报告:三个球队之间一共有n场比赛,现在已经进行了k场,不知道每个球队的胜场是多少,如三个球队的胜场 ...