UVa11995 I Can Guess the Data Structure!
思路:边读边模拟,注意empty的判断!
代码如下:
#include<iostream> #include<queue> #include<stack> using namespace std; int main(){ queue<int> q; priority_queue<int> pri_q; stack<int> sta; int n; while(cin>>n){ while(!q.empty()) q.pop(); //清空data while(!pri_q.empty()) pri_q.pop(); while(!sta.empty()) sta.pop(); int a,b,c; a=b=c=1; while(n--) { int op,x; cin>>op>>x; if(op == 1){ if(a) q.push(x); if(b) pri_q.push(x); if(c) sta.push(x); } else { if(a) if(q.empty()) a=0; else {a= q.front()==x; q.pop();} if(b) if(pri_q.empty()) b=0; else{b= pri_q.top()==x; pri_q.pop();} if(c) if(sta.empty()) c=0; else{c= sta.top()==x; sta.pop();} } } if(!a && !b &&!c) cout<<"impossible"; else if((a&&b) || (a&&c) ||(b&&c)) cout<<"not sure"; else{ if(a) cout<<"queue"; else if(b) cout<<"priority queue"; else cout<<"stack"; } cout<<" "; } return 0; }