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

原文网址:

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值