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

UVA 11995 - I Can Guess the Data Structure!【stl】

2015-11-17 20:49 337 查看
原文网址:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3146
挺恶心的一道题...需要判断多种状态,本来早写出来了,可是因为容器为空的时候,没特别注意,然后一re....

其实自己也在猜测是不是这个原因..........

需要判断三种容器,那么就直接把三种的状态全部模拟一下,然后各个判断,不过个人为了省些时间,直接判断三个,这样程序可读性下降了,不过还凑合........

然而事实上并没有省多少时间..........

#include<stdio.h>
#include<queue>
#include<stack>
using namespace std;
int main()
{
int n,m;
//	freopen("shuju.txt","r",stdin);
while(~scanf("%d",&n))
{
stack<int> s;
queue<int> q;
priority_queue<int> pq;
int cnt=0;
int kase[3]={0};// 0 栈,1 队列,2 优先队列
for(int i=0;i<n;++i)
{
int a,b;
scanf("%d%d",&a,&b);
if(a==1)
{
s.push(b);
q.push(b);
pq.push(b);
}
else
{
if(s.empty()||s.top()!=b)
{
kase[0]=1;
}
if(q.empty()||q.front()!=b)
{
kase[1]=1;
}
if(pq.empty()||pq.top()!=b)
{
kase[2]=1;
}
if(!s.empty())//好繁琐的三个判断
{
s.pop();
}
if(!q.empty())
{
q.pop();
}
if(!pq.empty())
{
pq.pop();
}
}
}
if(kase[0]&&kase[1]&&kase[2])
{
printf("impossible\n");
}
else if(!kase[0]&&kase[1]&&kase[2])
{
printf("stack\n");
}
else if(kase[0]&&!kase[1]&&kase[2])
{
printf("queue\n");
}
else if(kase[0]&&kase[1]&&!kase[2])
{
printf("priority queue\n");
}
else
{
printf("not sure\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: