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