给出一系列操作,包含操作数和操作码,判断符合这一系列操作返回值的数据结构类型(栈、队列、优先队列)
–
#include<bits/stdc++.h>
using namespace std;
const int _max = 1e3 + 10;
int n,op[_max],x[_max];//操作数与操作码
stack<int>stk;
queue<int>q;
priority_queue<int>pq;
bool isStack(){
while(!stk.empty())stk.pop();
for(int i = 0; i < n; ++ i){
if(op[i] == 1) stk.push(x[i]);
else{
if(stk.empty()) return false;
int v = stk.top();stk.pop();//栈顶top
if(v != x[i]) return false;
}
}
return true;
}
bool isQueue(){
while(!q.empty())q.pop();
for(int i = 0; i < n; ++ i){
if(op[i] == 1) q.push(x[i]);
else{
if(q.empty()) return false;
int v = q.front();q.pop();//队首front
if(v != x[i]) return false;
}
}
return true;
}
bool isPriority(){
while(!pq.empty())pq.pop();
for(int i = 0; i < n; ++ i){
if(op[i] == 1) pq.push(x[i]);
else{
if(pq.empty()) return false;
int v = pq.top();pq.pop();//堆顶top
if(v != x[i]) return false;
}
}
return true;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
while(scanf("%d",&n)==1){
for(int i = 0; i < n; ++ i)
scanf("%d%d",op+i,x+i);
bool ok1 = isStack(),ok2 = isQueue(),ok3 = isPriority();
if(ok1&&!ok2&&!ok3) {puts("stack");continue;}
if(!ok1&&ok2&&!ok3) {puts("queue");continue;}
if(!ok1&&!ok2&&ok3) {puts("priority queue");continue;}
if(!ok1&&!ok2&&!ok3) {puts("impossible");continue;}
puts("not sure");
}
return 0;
}
Ctrl + B
Ctrl + I
Ctrl + Q
Ctrl + L
Ctrl + K
Ctrl + G
Ctrl + H
Ctrl + O
Ctrl + U
Ctrl + R
Ctrl + Z
Ctrl + Y
版权声明:本文为博主原创文章,未经博主允许不得转载。
【模拟+数据结构】UVA 11995 I Can Guess the Data Structure!
原文地址:http://blog.csdn.net/u012717411/article/details/47832497