您的位置:首页 > 产品设计 > UI/UE

UVa 11995 - I Can Guess the Data Structure!

2018-02-02 21:37 483 查看

题意

一个包包, 可能是栈, 队列, 优先队列, 或其他

操作1代表存入数据

操作2代表拿出顶端/首端的一个元素

思路

本以为是水题, 居然还WA了两次

仔细思考 发现有个特殊情况没考虑到: 如果存入数量小于取出数, 那么一定是impossible !!!

AC代码

操作的时候要先判断容器是否非空吖

用完要清空容器吖

#include <iostream>
#include <cstdio>
#include <stack>
#include <queue>

using namespace std;

stack<int> a;
queue<int> b;
priority_queue<int> c;

int main()
{
int T, x, y;
int fa,fb,fc;
int one, two;
while( ~scanf("%d",&T) )
{
fa = 1, fb = 1, fc = 1;
one = 0, two = 0;
while(T--){
scanf("%d%d",&x,&y);
if( x == 1 ){
one++;
a.push(y);
b.push(y);
c.push(y);
}
else{
two++;
if( !a.empty() ){
if( a.top() != y )  fa = 0;
else a.pop();
}
if( !b.empty() ){
if( b.front() != y )  fb = 0;
else b.pop();
}
if( !c.empty() ){
if( c.top() != y )  fc = 0;
else c.pop();
}
}
}
if( !fa && !fb && !fc || (two > one) )   puts("impossible");
else if( fa + fb + fc > 1 )      puts("not sure");
else if( fa )      puts("stack");
else if( fb )      puts("queue");
else               puts("priority queue");
while( !a.empty() )     a.pop();
while( !b.empty() )    b.pop();
while( !c.empty() )    c.pop();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva 数据结构