【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!

2022-10-17,,,

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=;
while(n--) {
int op,x;
cin>>op>>x;
if(op == ){
if(a) q.push(x);
if(b) pri_q.push(x);
if(c) sta.push(x);
}
else {
if(a) if(q.empty()) a=; else {a= q.front()==x; q.pop();}
if(b) if(pri_q.empty()) b=; else{b= pri_q.top()==x; pri_q.pop();}
if(c) if(sta.empty()) c=; 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<<"\n";
}
return ;
}

暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!的相关教程结束。

《【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!.doc》

下载本文的Word格式文档,以方便收藏与打印。