2018.2.1【 UVA-11995 】解题报告(STL,数据结构,模拟)

5 篇文章 0 订阅
4 篇文章 0 订阅

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

Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!



【题目大意】

给你一组输入的数据与输出的数据,对比问符合哪种数据结构(stack,queue,priority_queue)。

【解题思路】

按照三种数据结构模拟输入输出,最后对比即可。

【解题代码】

#include <cstdio>
#include <cstring>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=10010;
int n;
int a[maxn],b[maxn];
int sta[maxn],que[maxn],pqu[maxn];
bool flag[3];
int main()
{
	while(~scanf("%d",&n))
	{
		stack <int> st;
		queue <int> qu;
		priority_queue <int> pq;
		memset(flag,0,sizeof(flag));
		int m=-1,kase=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&m,&b[i]);
			if(m==1)
			{
				st.push(b[i]);
				qu.push(b[i]);
				pq.push(b[i]);
			}
			else if(m==2)
			{
				a[kase]=b[i];
				if(!flag[0]&&!st.empty())//要注意判断是不是弹出多了 
				{
					sta[kase]=st.top();
					st.pop();
					if(sta[kase]!=a[kase]) flag[0]=1;
				} 
				else flag[0]=1;
				if(!flag[2]&&!pq.empty())//要注意判断是不是弹出多了 
				{
					pqu[kase]=pq.top();
					pq.pop();
					if(pqu[kase]!=a[kase]) flag[2]=1;
				} 
				else flag[2]=1;
				if(!flag[1]&&!qu.empty())//要注意判断是不是弹出多了 
				{
					que[kase]=qu.front();
					qu.pop();
					if(que[kase]!=a[kase]) flag[1]=1;
				} 
				else flag[1]=1;
				kase++;
			}
		}
		
//		for(int i=0;i<kase;i++)
//			printf("a %d   st %d  qu %d  pq %d \n",a[i],sta[i],que[i],pqu[i]);
//		打印
		if(!flag[0]&&flag[1]&&flag[2])
			printf("stack\n");
		else if(!flag[1]&&flag[0]&&flag[2])
			printf("queue\n");
		else if(!flag[2]&&flag[0]&&flag[1])
			printf("priority queue\n");
		else if(flag[0]&&flag[1]&&flag[2])
			printf("impossible\n");
		else printf("not sure\n"); 
	}
	return 0;
}
 


【收获与反思】

一开始一直RE,应该是因为数据里可能2的数量>1的数量,导致栈/队列/优先队列为空时还弹出,导致了RE,所以一定要做好判断检测。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值