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