UVA 11995 I Can Guess the Data Structure! STL
题目链接: UVA很难登上去吧......
题目大意: 给你几组输入与输出让你判断是栈,队列, 堆, 还是不确定, 还是哪种也不是
解题思路: 这题看起来很简单, 搞几个标准STL, 和结果一对比就知道了, 坑点就是栈, 队列或堆在判断.top的时候会溢出造成RE, 在这儿坑了两次, 一开始还以为是不是全局不行
代码:
#include <queue> #include <stack> #include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std;int ans[100]; stack<int> S; queue<int> Q; priority_queue<int> PQ;int main() {int n;while( scanf( "%d", &n ) != EOF ) {while( !S.empty() ) {S.pop();}while( !Q.empty() ) {Q.pop();}while( !PQ.empty() ) {PQ.pop();}ans[0] = ans[1] = ans[2] = 1;while( n-- ) {int op, num;scanf( "%d%d", &op, &num );if( op == 1 ) {S.push(num);Q.push(num);PQ.push(num);}else {if( ans[0] && !Q.empty() ) {int temp = Q.front();Q.pop();if( num != temp ) ans[0] = 0;}else ans[0] = 0;if( ans[1] && !S.empty() ) {int temp = S.top();S.pop();if( num != temp ) ans[1] = 0;}else ans[1] = 0;if( ans[2] && !PQ.empty() ) {int temp = PQ.top();PQ.pop();if( num != temp ) ans[2] = 0;}else ans[2] = 0;}}int sum = ans[0] + ans[1] + ans[2];if( sum == 1 ) {if( ans[0] == 1 ) {printf( "queue\n" );}else if( ans[1] == 1 ) {printf( "stack\n" );}else {printf( "priority queue\n" );}}else if( sum == 0 ) {printf( "impossible\n" );}else {printf( "not sure\n" );}}return 0; }
View Code
思考: 这题就是考细心的啊, 还记得去年第一次组队打多校的时候就是遇到了这个坑点, 总RE, 最后花了三个多小时才找到错误, 自己现在却还是不长记性, 所以以后遇到栈等需要取偷拍top或者pop操作的时候, 先判空!
转载于:https://www.cnblogs.com/FriskyPuppy/p/7240235.html
UVA 11995 I Can Guess the Data Structure! STL相关推荐
- uva 11995 I Can Guess the Data Structure!
https://vjudge.net/problem/UVA-11995 题意: 现在有一个未知的数据结构,给出n个操作,如果说操作为1的话,给出一个x放入这个结构,如果说操作为2的话,表示从里面拿出 ...
- uva 11995——I Can Guess the Data Structure!
题意:给定一个包,然后给定这个包的一些操作以此来判断包的数据结构类型. 思路:直接按照stl来模拟即可. code: #include <bits/stdc++.h> using name ...
- LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
- leetcode 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object):def __init__(self):"""initialize your data str ...
- TRIE - Data Structure
Introduction 介绍 Trie,又称单词查找树,是一种树形结构,用于保存大量的字符串.它的优点是:利用字符串的公共前缀来节约存储空间. Trie is an ordered tree dat ...
- 牛客小白月赛11:Rinne Loves Data Structure
Rinne Loves Data Structure 思路 我们插入的位置大概分了四种: 第一种 显然我们找到比当前插入的值的pre,也就是比当前节点大的最小值. 第二种 我们只要找到当前节点的suc ...
- HDU - 7072 Boring data structure problem 双端队列 + 思维
传送门 文章目录 题意: 思路: 题意: 你需要实现如下四个操作 q≤1e7q\le1e7q≤1e7 思路: 做的时候想了个链表的思路让队友写了,懒. 看了题解感觉题解还是很妙的. 你需要快速插入一个 ...
- HDU - 6967 G I love data structure 线段树维护矩阵 + 细节
传送门 文章目录 题意: 思路: 题意: 给你两个长度为nnn的数组a,ba,ba,b,你需要完成如下四种操作: 思路: 思路还是比较简单的,首先建一颗线段树,线段树中维护a,b,a2,b2,aba, ...
最新文章
- SOC,System on-a-Chip技术初步
- yolov5模型训练
- 自考第一章计算机基础知识,2018年自考计算机网络技术基础复习资料:第一章...
- c语言将0到1十等分放入数组,C语言课程设题计目汇总.doc
- Unity 脚本生命周期流程图
- 使用MegaCli工具,在线调整raid配置
- qml 时间控件_Qt编写自定义控件54-时钟仪表盘
- python3.7操作kafka_python操作kafka
- Google docs支持上传任何文档包括pdf...Cool!
- android word缺少字体下载,OfficeSuite字体包
- java 代码重构 pdf_《重构:改善既有代码的设计》 PDF 下载
- 史上最完整的Android开发工具集合(附带下载地址)
- AD7705模块设计(附源码)
- 实训日记(一)——剧本
- Python:实现simpson rule辛普森法则算法(附完整源码)
- 阿里云上实现DDNS公网解析
- java opencv 实现换脸
- flash 第六章 课后练习
- ContentPane
- windows11怎么截屏
热门文章
- Eclipse转Intellij IDEA
- Hbase shell详情
- Brodatz纹理图像库
- 您能看出这个生成缩略图的方法有什么问题吗?
- Stanford UFLDL教程 数据预处理
- Stanford UFLDL教程 微调多层自编码算法
- 斯坦福大学机器学习第十课“应用机器学习的建议(Advice for applying machine learning)”
- Java并发编程——volatile
- 技术大牛长成记之不要光看热闹
- 【点击模型学习笔记】Predicting Clicks_Estimating the Click-Through Rate for New Ads_MS_www2007...