UVA-11995
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 isa stack (Last-In, First-Out),
a queue (First-In, First-Out), a priority-queue (Always take out largerelements first) or something else that you can hardly imagine!InputThere 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 xis always a positive integer not larger than 100. The input is terminated by end-of-file
(EOF).OutputFor 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 mentionedabove.Sample Input61 11 21 32 12 22 361 11 21 32 32 22 121 12 241 21 12 12 271 21 51 11 32 51 42 4Sample Outputqueuenot sureimpossiblestackpriority queue
题解:分别定义 stack、queue、priority_queue 判断这个操作序列是不是符合上述结构。
AC代码为:
#include<stdio.h>
#include<stack>
#include<queue>
using namespace std;
int main()
{
int n, i, x, y, f[4];
while(~scanf("%d",&n))
{
stack<int> s;
queue<int> q;
priority_queue<int, vector<int>, less<int> > pq;
for(i=0;i<3;i++)
f[i]=1;
for(i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
if(x == 1)
{
s.push(y);
q.push(y);
pq.push(y);
}
else
{
if(!s.empty())
{
if(s.top() != y)
f[0] = 0;
s.pop();
}
else
f[0] = 0;
if(!q.empty())
{
if(q.front() != y)
f[1] = 0;
q.pop();
}
else
f[1] = 0;
if(!pq.empty())
{
if(pq.top() != y)
f[2] = 0;
pq.pop();
}
else
f[2] = 0;
}
}
int num = 0;
for(i = 0; i < 3; i++)
if(f[i] == 1)
num++;
if(num == 0)
printf("impossible\n");
else if(num > 1)
printf("not sure\n");
else
{
if(f[0] == 1)
printf("stack\n");
else if(f[1] == 1)
printf("queue\n");
else
printf("priority queue\n");
}
}
return 0;
}
UVA-11995的更多相关文章
- [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 ...
- 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!(数据结构练习)
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里的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!(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!(模拟)
思路:分别定义栈,队列,优先队列(数值大的优先级越高).每次放入的时候,就往分别向三个数据结构中加入这个数:每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结 ...
- 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. ...
- UVA - 11995 模拟
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #i ...
- UVA 11995 STL 使用
There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...
随机推荐
- ArcGIS API For Javascript:新增热力图层的方法
当我们要制作一个热力图层,可以通过以下 3 步来实现: 引入类 在 require 中需引入 "esri/layers/FeatureLayer", "esri/rend ...
- django_1:配置文件
工程下: settings.py(建议设置成如下) DATABASES #数据库配置 DEBUG = True ...
- 四 linuk常用命令 1. 文件处理命令
一. 命令格式与目录处理命令ls 命令格式 命令格式:命令 [-选项] [参数] 例:ls -la /etc 说明: 1.个别命令使用不遵循此格式 2. 当有多个选项时,可以写在一起 3.简化选项与完 ...
- F#周报2019年第47期
新闻 相遇WebWindow,.NET Core上的跨平台webview类库 使用Bolero在WebAssembly中运行F# 用于你团队代码库的AI辅助IntelliSense Jupyter N ...
- 你必须知道的容器日志 (2) 开源日志管理方案 ELK
本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章.上一篇<你必须知道的容器日志(1)>中介绍了Docker自带的log ...
- Cygwin安装教程
cygwin是一个在windows平台上运行的unix模拟环境,是cygnus solutions公司开发的自由软件. 它对于学习unix/linux操作环境,或者从unix到windows的应用程序 ...
- PHP中echo与print语句的实例教程
在 PHP 中,有两种基本的输出方法:echo 和 print. echo与print的差异 echo能够输出一个以上的字符串. print只能输出一个字符串,并始终返回 1. 提示:echo 比 p ...
- Python常见字符串方法函数
1.大小写转换 S.lower() S.upper() 前者将S字符串中所有大写字母转为小写,后者相反 S.title() S.capitalize() 前者返回S字符串中所有单词首字母大写且其他字母 ...
- python接口设计中的__all__和del
最近在实现python接口中遇到了一些小问题,解决后总结如下. 目的:在设计接口时,只暴露某个文件的特定方法. 例如: t.py import os import sys def a(): pass ...
- PowerMock学习(九)之Mock Answer的使用
关于Mock Answer 上一篇文章,有介绍过关于Arguments Matche的使用,其实 Answer的作用与其比较类似,但是它比 Arguments Matcher 更加强大. Argume ...