Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

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

$
0
0

题目链接:

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

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









作者:a1006570862 发表于2013-11-27 0:19:25 原文链接
阅读:114 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>