题意: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;
}