E

There is a bag-like data structure, supporting two operations:1 x Throw an element x into the bag.2 Take out an element from the bag.Given a sequence of operations with return values, you’re going to guess the data structure. It isa stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out largerelements first) or something else that you can hardly imagine!InputThere are several test cases. Each test case begins with a line containing a single integer n (1 ≤ n ≤1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x.That means after executing a type-2 command, we get an element x without error. The value of xis always a positive integer not larger than 100. The input is terminated by end-of-file (EOF).OutputFor each test case, output one of the following:stack It’s definitely a stack.queue It’s definitely a queue.priority queue It’s definitely a priority queue.impossible It can’t be a stack, a queue or a priority queue.not sure It can be more than one of the three data structures mentionedabove.

Sample Input

6

1 1

1 2

1 3

2 1

2 2

2 3

6

1 1

1 2

1 3

2 3

2 2

2 1

2

1 1

2 2

4

1 2

1 1

2 1

2 2

7

1 2

1 5

1 1

1 3

2 5

1 4

2 4

Sample Output

queue

not sure

impossible

stack

priority queue

这题没什么了就是对应的STL的演示,定义不同的类型,然后按操作来,看最后结果对不对应就可以了。这里注意优先队列的开头,和队列的一样用#include<queue>即可。

ac代码:

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
int main()
{
int n;
int p, q;
while (cin >> n)
{
stack<int> a;
queue<int> b;
priority_queue<int, vector<int>, less<int>>c;
int temp1 = 1, temp2 = 1, temp3 = 1;
for (int i = 0; i < n; i++)
{
cin >> p >> q;
if (p == 1)
{
a.push(q);
b.push(q);
c.push(q);
}
else
{
if (!a.empty())
{
if (a.top() != q)
temp1 = 0;
a.pop();
}
else
temp1 = 0;
if (!b.empty())
{
if (b.front() != q)
temp2 = 0;
b.pop();
}
else
temp2 = 0;
if (!c.empty())
{
if (c.top() != q)
temp3 = 0;
c.pop();
}
else
temp3 = 0;
}
}
int count = 0;
if (temp1 == 1)
count++;
if (temp2 == 1)
count++;
if (temp3 == 1)
count++;
if (count == 0)
cout << "impossible" << endl;
else if (count > 1)
cout << "not sure" << endl;
else
{
if (temp1 == 1)
cout << "stack" << endl;
else if (temp2 == 1)
cout << "queue" << endl;
else
cout << "priority queue" << endl;
}
}
return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值