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 ...
随机推荐
- Linux指令--ln
ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在 ...
- Linkin大话Java和internet概念
整理电脑,无意中翻到不知道哪里来的文章,觉得里面写的很好,仔细看过一遍后,整理了下贴了出来,其中的好多概念我觉得讲的很透彻. 既然Java不过另一种类型的程序设计语言,为什么还有这么多的人认为它是计算 ...
- MySql全文索引
使用索引是数据库性能优化的必备技能之一.在MySQL数据库中,有四种索引:聚集索引(主键索引).普通索引.唯一索引以及我们这里将要介绍的全文索引(FULLTEXT INDEX). 全文索引(也称全文检 ...
- z-index在IE中的坑
在CSS2.1规范中,每个盒模型的位置是三维的,分别是平面画布上的x轴,y轴以及表示层叠的z轴.对于每个html元素,都可以通过设置z-index属性来设置该元素在视觉渲染模型中的层叠顺序. z-in ...
- awk匹配某一段内容,打印第一段
要求: awk 文本在文本中搜索abc搜到后再从搜到的那一行开始一直输出后面的行,直到某一行含有bcd就停止 测试文本: [root@localhost]# cat awktest sadfj sdj ...
- vim批量注释
vim批量注释 法一.在vim中 :20,30 s/^/#/g 20-30行 用 # 注释掉.(python是用#注释地---) :20,30 s/^#//g 20-30行 取消注释 法二. 1.多行 ...
- eclipse修改默认workspace
1.进入 Window > Preferences > General > Startup and Shutdown 选中 Prompt for workspace on start ...
- iOS-隐藏Navigation导航栏线
去除navigationBar上那条线: ///隐藏navigationBar导航栏线(直接写在UINavigationController-viewDidLoad方法里面即可) UIView *ba ...
- CAS基础和原子类
基于CAS实现的AtomicInteger. AtomicLong. AtomicReference. AtomicBoolean也被称为乐观锁. CAS的语义是“我认为V的值应该为A,如果是,那么将 ...
- BZOJ 1801: [Ahoi2009]chess 中国象棋 [DP 组合计数]
http://www.lydsy.com/JudgeOnline/problem.php?id=1801 在N行M列的棋盘上,放若干个炮可以是0个,使得没有任何一个炮可以攻击另一个炮. 请问有多少种放 ...