UVA - 11995:I Can Guess the Data Structure!

I Can Guess the Data Structure!

来源:UVA

标签:数据结构

参考资料:

相似题目:

题目

There is a bag-like data structure, supporting two operations:

Given a sequence of operations with return values, you’re going to guess the data structure. It is
a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger
elements first) or something else that you can hardly imagine!

输入

There are several test cases. Each test case begins with a line containing a single integer n (1 ≤ n ≤ 1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x. That means after executing a type-2 command, we get an element x without error. The value of x is always a positive integer not larger than 100. The input is terminated by end-of-file (EOF).

输出

For each test case, output one of the following:
UVA - 11995:I Can Guess the Data Structure!_第1张图片

输入样例

6
1 1
1 2
1 3
2 1
2 2
2 3
6
1 1
1 2
1 3
2 3
2 2
2 1
2
1 1
2 2
4
1 2
1 1
2 1
2 2
7
1 2
1 5
1 1
1 3
2 5
1 4
2 4

输出样例

queue
not sure
impossible
stack
priority queue

参考代码

#include
#include
#include
#define MAXN 1000
using namespace std;

int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		int type, num;
		int flag=7, sf=1, qf=2, pqf=4;
		stack<int> s;
		queue<int> q;
		priority_queue<int> pq;
		for(int i=0;i<n;i++){
			scanf("%d%d",&type,&num);
			if(type==1){
				s.push(num);
				q.push(num);
				pq.push(num);
			}
			else{
				if(sf){
					if(s.empty() || !s.empty()  && s.top()!=num){
						flag-=sf;
						sf=0;		
					}else s.pop();
				}
				if(qf){
					if(q.empty() || !q.empty()  && q.front()!=num){
						flag-=qf;
						qf=0;		
					}else q.pop();
				}
				if(pqf){
					if(pq.empty() || !pq.empty()  && pq.top()!=num){
						flag-=pqf;
						pqf=0;		
					}else pq.pop();
				}
			}
		}
		if(flag==1){
			printf("stack\n");
		}else if(flag==2){
			printf("queue\n");
		}else if(flag==4){
			printf("priority queue\n");
		}else if(flag>0){
			printf("not sure\n");
		}else 
			printf("impossible\n");
	}
	return 0;
}

你可能感兴趣的:(【记录】算法题解)