UVA - 11995 I Can Guess the Data Structure!(模拟)
思路:分别定义栈,队列,优先队列(数值大的优先级越高)。每次放入的时候,就往分别向三个数据结构中加入这个数;每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结构,可以用三个标志变量来标志可能的数据结构。
注意:可能会出现数据结构为空的情况,要先判断非空才能取元素。
AC代码
#include <stdio.h>
#include <stack>
#include <queue>
using namespace std;
//分别定义栈,队列,优先队列,然后模拟
stack<int> sta;
queue<int> que;
priority_queue<int> p_que;
void init() {
while(!sta.empty()) sta.pop();
while(!que.empty()) que.pop();
while(!p_que.empty()) p_que.pop();
}
int main() {
int n;
while(scanf("%d", &n) == 1) {
init();
int tag1 = 1, tag2 = 1, tag3 = 1;
for(int i = 0; i < n; ++i) {
int tag, num;
scanf("%d %d", &tag, &num);
if (tag == 1) {
sta.push(num);
que.push(num);
p_que.push(num);
} else if(tag == 2) {
if (sta.empty() || sta.top() != num) {
tag1 = 0;
} else sta.pop();
if (que.empty() || que.front() != num) {
tag2 = 0;
} else que.pop();
if (p_que.empty() || p_que.top() != num) {
tag3 = 0;
} else p_que.pop();
}
}
int res = tag1 + tag2 + tag3;
if (res == 0) {
printf("impossible\n");
} else if(res >= 2) {
printf("not sure\n");
} else {
if (tag1) {
printf("stack\n");
} else if (tag2) {
printf("queue\n");
} else printf("priority queue\n");
}
}
return 0;
}
如有不当之处欢迎指出!
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!(数据结构练习)
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!(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 ...
- HDU 5929 Basic Data Structure 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 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 ...
随机推荐
- MySQL两大存储引擎InnoDB与MyISAM
1.InnoDB存储引擎 MySQL5.5中InnoDB成为默认的存储引擎.InnoDB是事务型存储引擎,被设计用来处理大量的短期事务.它的性能和自动崩溃恢复特性,使得它在非事务场景中也很流行. 所以 ...
- Log4j扩展使用--日志格式化器Layout
Layout:格式化输出日志信息 OK,前面我已经知道了.Appender必须使用一个与之相关联的Layout,这样才能知道怎样格式化输出日志信息. 日志格式化器Layout负责格式化日志信息,方法l ...
- 一步步使用BMC Atrium Orchestrator Vmware Infrastructure Event Monitor
本教程将一步步演示怎么使用BMC Atrium Orchestrator (BAO) Vmware Infrastructure Event Monitor来监控VSphere Webservice的 ...
- 函数式编程--为什么会出现lambda表达式?
java一直处在发张和演化的过程中,其中有2个版本从根本上改变了代码的编写方式.第一个就是JDK5之后增加的泛型,还有一个就是现在介绍的函数式编程,lambda表达式. lambda表达式是java8 ...
- maven系列--settings.xml
安装maven,会有一份settings.xml文件,我们可以理解这份文件就是用来管理本地maven的一系列东西的.这里我先贴出一份完整的原始xml. <?xml version="1 ...
- POI--HSSFCellStyle类
通过POI来进行单元格格式的设定 设定格式使用「HSSFCellStyle」类.它有一个构造方法: protected HSSFCellStyle(short index, ExtendedForma ...
- linkin大话设计模式--命令模式
linkin大话设计模式--命令模式 首先考虑一种应用情况,某个方法需要完成某一个功能,这个功能的大部分功能已经确定了,但是有可能少量的步骤没法确定,必须等到执行这个方法才可以确定. 也就是说,我们写 ...
- 《struts2》:指定多个配置文件和默认Action
转载:http://m.blog.csdn.net/article/details?id=51212968
- zabbix图形乱码
毕竟是中文为主,特别是有些香项目最好以中文命名,容易区分,也方便识别 环境: centos7.3安装zabbix3.2 问题: 图文乱码问题 原理上只要找到对应的字符集,在修改配置文件 windows ...
- 小谈ConcurrentHashMap
面试的时候被面试官问了点相关知识,再次记录一些自己的总结 一. 1.HashTable也可实现线程安全,但是它是用synchronized实现的,所以其他线程访问HashTable的同步方法时,可能会 ...