UVA11995 优先队列

I Can Guess the Data Structure!

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

1 x

Throw an element x into the bag.

2

Take out an element from the bag.

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!

Input

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). The size of input file does not exceed 1MB.

Output

For each test case, output one of the following:

stack

It's definitely a stack.

queue

It's definitely a queue.

priority queue

It's definitely a priority queue.

impossible

It can't be a stack, a queue or a priority queue.

not sure

It can be more than one of the three data structures mentioned above.

Sample Input

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

Output for the Sample Input

queue
not sure
impossible
stack
priority queue



遇到的问题和解题思路:

       首先看到这道题以为是单纯的水题,然后写了2个小时没写出来。看了一下别人写的代码以后发现,原来是理解出错了TAT。

       这道题的我理解错的地方在于,就是你输入进去的东西是一个一个的读入的,而不是全部读入了以后再比较,所以说TAT我就错在这里了。


给出AC代码:


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>

using namespace std;

int n, st, qu, pr;
int a[1005][2];
queue<int> que;
stack<int> s;
priority_queue<int> pq;

void solve(int x ){
	if(a[x][0] == 1){
		que.push(a[x][1]);
		s.push(a[x][1]);
		pq.push(a[x][1]);
	}
	else {
		if(que.empty())qu = 0;
		else{
			if(que.front() != a[x][1]){
				que.pop();
				qu = 0;
			}
			else que.pop();
		}
		if(s.empty())st = 0;
		else {
			if(s.top() != a[x][1]){
				s.pop();
				st = 0;
			}
			else s.pop();
		}	
		if(pq.empty())pr = 0;
		else{
			if(pq.top() != a[x][1]){
				pq.pop();
				pr = 0;
			}	
			else pq.pop();
		}
	}
}

int main(){
	while(scanf("%d", &n)!=EOF){
		int i, j;
		st = qu = pr = 1;

		while(!que.empty())que.pop();
		while(!s.empty())s.pop();
		while(!pq.empty())pq.pop();
		for (i = 0; i < n; i++){
			for (j = 0; j < 2; j++){
				scanf("%d",&a[i][j]);
			}
			solve (i );
			//printf("%d %d %d\n", st, qu, pr);
		}
		if(st + qu + pr > 1)printf("not sure\n");
		else if(st + qu + pr == 0)printf("impossible\n");
		else{
			if(st == 1)printf("stack\n");
			else if(qu == 1)printf("queue\n");
			else if(pr == 1)printf("priority queue\n");
		}
	}
	return 0;
}


给出测试数据,基本上所有讨论的种类都有了:(应该吧)

6
1 1
1 2
1 3
1 4
1 5
2 1
6
1 1
1 2
1 3
1 4
1 5
2 5
6
1 5
1 4
1 3
1 2
1 1
2 5
6
1 5
1 4
1 3
1 2
1 1
2 1
5
1 3
1 2
2 3
2 2
2 1
4
1 3
2 3
2 2
1 2/*最后一种,就是impossible,因为先是一个1,所以输入了3,后来说连着消除两次,显然这个是不可能的事情,所以是impossible*/



之前写的代码其中有两个问题,其中一个就是在每个函数中的x和y的值得大小不确定,当y>x的时候,就会出现RE。还有一个就是之前说的,题目理解错误

下面给出错误的代码,来警示错误:


#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>

using namespace std;

int n;
int a[1005], b[1005];
int ty[5];
int pend, x, y;

bool queue1(){
	queue<int> que;
	for(int i = 0; i < x; i++)
	    que.push(a[i]);
    for(int i = 0; i < y; i++){
    	int d = que.front();
    	que.pop();
    	if(d != b[i])return false;
    }
    ty[2] = 1;
    return true;
}

bool stack1(){
	stack<int> s;
	for(int i = 0; i < x; i++) s.push(a[i]);
	for(int i = 0; i < y; i++){
		int d = s.top();
		s.pop();
		if(d != b[i])return false;
	}
	ty[1] = 1;
	return true;
}

bool priority1(){
	priority_queue<int> pq;
	for(int i = 0; i < x; i++)pq.push(a[i]);
	for(int i = 0; i < y; i++){
		int d = pq.top();
		pq.pop();
		if(d != b[i])return false;
	}
	ty[3] = 1;
	return true;
}


int main(){
	while(scanf("%d", &n)!=EOF){
		x = 0, y = 0;
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		memset(ty, 0, sizeof(ty));
		for(int i = 0; i < n; i++){
			scanf("%d",&pend);
			if(pend == 1)scanf("%d",&a[x++]);
			if(pend == 2)scanf("%d",&b[y++]);
		}
		int res = 0;
		res += queue1();
		res += stack1();
		res +=priority1();
		if(res >= 2)printf("not sure\n");
		else if(res == 0)printf("impossible\n");
		else if(res == 1){
				if(ty[1])printf("stack\n");
				else if(ty[2])printf("queue\n");
				else if(ty[3])printf("priority queue\n");
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值