Uva 11995 - I Can Guess the Data Structure! (判断数据类型)

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=229&page=show_problem&problem=3146


题目分析:

先设定一个结构体数组存储输入的数据,在定义三个函数判断是否为栈,队列,优先队列,再对返回值进行判断。

另外,在结构体里面定义友元函数,用来优先队列实现里面判断大小,注意,参数列表不能用 data &a, data &b会报错


代码:

#include<iostream>
#include<stack>
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;

struct Data
{
	int k, v;
	friend bool operator < (Data a, Data b){
    return a.v < b.v;
  }
}d[1010];
int n;

int fstack(){
	 stack<int> s;
	 for (int i = 0; i < n; ++i)
	{
		if (d[i].k == 1) s.push(d[i].v);
		if (d[i].k == 2) 
		{
			if (s.empty() == 1) return 0;
			if (s.top() == d[i].v)
			{
				s.pop(); 
				continue;
			}
			else return 0;
		}
	}
	  return 1;
}
int fqueue(){
	queue<int> qq;
	for (int i = 0; i < n; ++i)
	{
		if (d[i].k == 1) qq.push(d[i].v);
		if (d[i].k == 2) 
		{
			if (qq.empty() == 1) return 0;
			if (qq.front() == d[i].v) {
				qq.pop(); 
				continue;
			 }
		 else return 0;
		}
	}
	 return 1;
}
int fpriqueue(){
	priority_queue<Data> qq;
	for (int i = 0; i < n; ++i)
	{
		if (d[i].k == 1) qq.push(d[i]);
		if (d[i].k == 2) 
		{
			if (qq.empty() == 1) return 0;
			if (qq.top().v == d[i].v)
			{
				qq.pop(); continue;
			}
			else return 0;
		}
  }
  return 1;
}


int main(){
	while(~scanf("%d", &n))
	{
		for (int i = 0; i < n; ++i)
		{
			scanf("%d%d", &d[i].k, &d[i].v);
		}
		int fs = fstack(), fq = fqueue(), fpri = fpriqueue();

		 if (fs + fq + fpri > 1) printf("not sure\n");
		else if (fs) printf("stack\n");
		else if (fq) printf("queue\n");
		else if (fpri) printf("priority queue\n");
		else printf("impossible\n");
	}
	return 0;
}


用while(scanf("%d", &n))的话会超时 1.00s

用while(~scanf("%d", &n))的话只要 0.04

差距~~~~(>_<)~~~~ 







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值