UVa 11995 I Can Guess the Data Structure!
做道水题凑凑题量,=_=||。
直接用STL里的queue、stack 和 priority_queue模拟就好了,看看取出的元素是否和输入中的相等,注意在此之前要判断一下是否非空。
#include <bits/stdc++.h>
using namespace std; void scan( int &x )
{
char c;
while( c = getchar(), c < '' || c > '' );
x = c - '';
while( c = getchar(), c >= '' && c <= '' ) x = x* + c - '';
} const int maxn = + ;
int t[maxn], d[maxn]; bool is_queue(int n)
{
queue<int> Q;
for(int i = ; i < n; i++)
{
if(t[i] == ) Q.push(d[i]);
else
{
if(Q.empty()) return false;
int x = Q.front(); Q.pop();
if(x != d[i]) return false;
}
}
return true;
} bool is_stack(int n)
{
stack<int> S;
for(int i = ; i < n; i++)
{
if(t[i] == ) S.push(d[i]);
else
{
if(S.empty()) return false;
int x = S.top(); S.pop();
if(x != d[i]) return false;
}
}
return true;
} bool is_p_queue(int n)
{
priority_queue<int> Q;
for(int i = ; i < n; i++)
{
if(t[i] == ) Q.push(d[i]);
else
{
if(Q.empty()) return false;
int x = Q.top(); Q.pop();
if(x != d[i]) return false;
}
}
return true;
} int main()
{
//freopen("in.txt", "r", stdin); int n;
while(scanf("%d", &n) == )
{
for(int i = ; i < n; i++) { scan(t[i]); scan(d[i]); }
bool f1 = is_queue(n);
bool f2 = is_stack(n);
bool f3 = is_p_queue(n);
int cnt = (int)f1 + (int)f2 + (int)f3;
if(cnt > ) puts("not sure");
else if(cnt == ) puts("impossible");
else
{
if(f1) puts("queue");
if(f2) puts("stack");
if(f3) puts("priority queue");
}
} return ;
}
代码君
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 stack,queue,priority_queue
题意:给你n个操做,判断是那种数据结构. #include<iostream> #include<cstdio> #include<cstdlib> #includ ...
- UVA - 11995 I Can Guess the Data Structure!(模拟)
思路:分别定义栈,队列,优先队列(数值大的优先级越高).每次放入的时候,就往分别向三个数据结构中加入这个数:每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结 ...
- 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 ...
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
随机推荐
- 从一个新手容易混淆的例子简单分析C语言中函数调用过程
某天,王尼玛写了段C程序: #include <stdio.h> void input() { int i; ]; ; i < ; i++) { array[i] = i; } } ...
- Careercup - Facebook面试题 - 5179916190482432
2014-05-01 00:45 题目链接 原题: input [,,,] output [,,,] Multiply all fields except it's own position. Res ...
- NopCommerce——源代码的组织,以及系统的架构
近来使用NopCommerce进行开发,仿照源码的Demo也能做出看上去还蛮高端大气上档次的系统出来,现下准备深入学习学习.首先从官方的Documentation开始看起,先来一篇官网文章的翻译(园里 ...
- AsyncTask不能同时运行多个实例解决办法
在项目中使用AsyncTask时,发现创建的多个实例无法同时运行,比如: AsyncTask t1 = new MyTask(); AsyncTask t2 = new MyTask(); t1.ex ...
- Linq常用
1.左关联查询var lst = from m in db.信息 join d in db.明细信息 on m.单号 equals d.单号 into mi ...
- win下Maven安装和基本设置
注:本文介绍 Windows 平台上 Maven 的安装.Maven 3 需要运行在 JDK1.4 以上的版本上. 非原创:原创地址 http://www.ibm.com/developerworks ...
- iOS开发之ARC&MRC混用
Xcode 项目中我们可以使用 ARC 和非 ARC 的混合模式. 如果你的项目使用的非 ARC 模式,则为 ARC 模式的代码文件加入 -fobjc-arc 标签. 如果你的项目使用的是 ARC 模 ...
- PHP之implode与explode函数讲解
implode (PHP 4, PHP 5) implode — 将一个一维数组的值转化为字符串 说明¶ string implode ( string $glue , array $pieces ) ...
- Javascript中appendChilid()内涵
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- hdu 2481 Toy
好题!!!没话说…… 用到的知识面很多,这题的难点在于公式的推导. 原始推导过程见:http://hi.baidu.com/spellbreaker/item/d8bb3bda5af30be6795d ...