您的位置:首页 > 产品设计 > UI/UE

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

2013-11-27 00:19 696 查看
题目链接:

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

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





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UVA11995 ACM