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. ...
随机推荐
- IDEA升级,提示"Connection Error Failed to prepare an update"
问题来源: 之前修改了IDEA的默认配置文件路径,然后升级新版本时就无法升级,提示"Failed to prepare an update Temp directory inside ins ...
- SpringBoot之微服务日志链路追踪
SpringBoot之微服务日志链路追踪 简介 在微服务里,业务出现问题或者程序出的任何问题,都少不了查看日志,一般我们使用 ELK 相关的日志收集工具,服务多的情况下,业务问题也是有些难以排查,只能 ...
- tomcat 日志(2)
一.Log4j在Tomcat中的配置说明(tomcat6) 学习Java中,从简单的开始.如果需要文中提到的文件可以找我要. http://www.apache.org/dist/tomcat/tom ...
- SSM配置梳理
这两天梳理了一下 SSM 的配置,做一个小总结 可能有一些不对的地方,如果您发现了什么错误,非常希望能帮忙指出,谢谢 我参考了很多文章,都标明了来源(链接),可能会影响阅读的连贯性,抱歉 ...
- nginx 根据不同url转发请求对应tomcat容器
根据前端请求的url,nginx转发到指定的tomcat容器 原理如图: 现在我们有2个tomcat,一个tomcat的端口为9001,另一个tomcat的端口为9002 1.找到nginx的配置文件 ...
- [Collection] 的常用方法有这些。
- 领扣(LeetCode)移动零 个人题解
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作, ...
- PL真有意思(三):名字、作用域和约束
前言 这两篇写了词法分析和语法分析,比较偏向实践.这一篇来看一下语言设计里一个比较重要的部分:名字.在大部分语言里,名字就是标识符,如果从抽象层面来看名字就是对更低一级的内存之类的概念的一层抽象.但是 ...
- IDEA导入MySQL的jdbc驱动,并操作数据库
将MySQL的jdbc驱动,导入IDEA的方式,虽然也能连接并且操作数据库,但并不推荐这种方式,推荐使用Maven工程的方式:https://www.cnblogs.com/dadian/p/1193 ...
- 音视频入门-14-JPEG文件格式详解
* 音视频入门文章目录 * JPEG 文件格式解析 JPEG 文件使用的数据存储方式有多种.最常用的格式称为 JPEG 文件交换格式(JPEG File Interchange Format,JFIF ...