uva 11995(stl)

题意:1 x,表示放进x元素,2表示拿出一个元素,给出n条指令,然后2 x表示取出的数据是什么,问可以从输入输出判断出是哪种数据结构(栈,队列,优先队列),如果有多种满足,就输出not sure,都不是就输出impossible。

题解:直接定义三个数据结构的stl变量,然后模拟放入数据,到拿出数据时和三种比对判断,可以知道是哪种数据结构。


#include <stdio.h>
#include <queue>
#include <stack>
using namespace std;
const int N = 1000;
int n;
queue<int> q1;
stack<int> s;
priority_queue<int> q2;

void init() {
	while (!q1.empty())
		q1.pop();
	while (!q2.empty())
		q2.pop();
	while (!s.empty())
		s.pop();
}

int main() {
	while (scanf("%d", &n) == 1) {
		int a, x, num1 = 0, num2 = 0, num3 = 0, cnt = 0;
		init();
		for (int i = 0; i < n; i++) {
			scanf("%d%d", &a, &x);
			if (a == 1) {
				q1.push(x);
				q2.push(x);
				s.push(x);
			}
			else {
				cnt++;
				int temp1 = -1, temp2 = -1, temp3 = -1;
				if (!q1.empty()) {
					temp1 = q1.front();	
					q1.pop();
				}
				if (!q2.empty()) {
					temp2 = q2.top();
					q2.pop();
				}
				if (!s.empty()) {
					temp3 = s.top();
					s.pop();
				}
				if (temp1 == x)
					num1++;
				if (temp2 == x)
					num2++;
				if (temp3 == x)
					num3++;
			}
		}
		if (num1 == cnt && num2 == cnt || num2 == cnt && num3 == cnt || num1 == cnt && num3 == cnt)
			printf("not sure\n");
		else if (num1 == cnt)
			printf("queue\n");
		else if (num2 == cnt)
			printf("priority queue\n");
		else if (num3 == cnt)
			printf("stack\n");
		else
			printf("impossible\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值