uva 11995 I Can Guess the Data Structure!

本文介绍了一个编程挑战,通过模拟操作来判断给定序列是否能由栈、队列或优先队列实现。使用C++实现了一个程序,根据输入的操作序列判断其符合哪种数据结构的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:I Can Guess the Data Structure!


思路:模拟。


代码:

#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<deque>
using namespace std;

int n;

int main() {

	while(~scanf("%d",&n)) {
		bool fstack=1,fque=1,fpri=1;
		stack<int> a;
		queue<int> b;
		priority_queue<int> c;
		for(int i=1; i<=n; i++) {
			int x,y;
			scanf("%d%d",&x,&y);
			if(x==1) {
				a.push(y);
				b.push(y);
				c.push(y);
			}
			if(x==2) {
				if(fstack==0||a.empty()||a.top()!=y) fstack=0;
				else a.pop();
				if(fque==0||b.empty()||b.front()!=y) fque=0;
				else b.pop();
				if(fpri==0||c.empty()||c.top()!=y) fpri=0;
				else c.pop();
			}
		}
		
		if(fstack==0&&fque==0&&fpri==0) printf("impossible");
		else if(fstack==1&&fque==0&&fpri==0) printf("stack");
		else if(fstack==0&&fque==1&&fpri==0) printf("queue");
		else if(fstack==0&&fque==0&&fpri==1) printf("priority queue");
		else printf("not sure");
		printf("\n");
		
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值