例3.1 猜猜数据结构 UVa11995
1.标题叙述性说明:点击打开链接
2.解题思路:据来推測一种可能的数据结构,备选答案有“栈,队列。优先队列”。结果也可能都不是或者不确定。
STL中已经有这三种数据结构了,因此直接模拟题意,输出时推断是否相应就可以。注意:弹出时要推断一下是否已经为空。
3.代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;
stack<int>s;
queue<int>q;
priority_queue<int>p;
char st[][20] = { "impossible", "priority queue", "queue","not sure" , "stack", "not sure","not sure" ,"not sure" };
void clear()
{
while (!s.empty())s.pop();
while (!q.empty())q.pop();
while (!p.empty())p.pop();
}
void add(int x)
{
s.push(x);
q.push(x);
p.push(x);
}
void pop()
{
if (!s.empty())s.pop();
if (!q.empty())q.pop();
if (!p.empty())p.pop();
}
int main()
{
//freopen("t.txt", "r", stdin);
int n;
while (~scanf("%d", &n))
{
clear();
int ok1, ok2, ok3;
ok1 = ok2 = ok3 = 1;
for (int i = 0; i < n; i++)
{
int a, b;
cin >> a >> b;
if (a == 1)add(b);//统一加入
else
{
if (s.empty() || s.top() != b)
ok1 = 0;
if (q.empty() || q.front() != b)
ok2 = 0;
if (p.empty() || p.top() != b)
ok3 = 0;
pop();//统一弹出
}
}
int x = (ok1 << 2) | (ok2 << 1) | ok3;//编码,方便输出结果
printf("%s\n", st[x]);
}
return 0;
}版权声明:本文博客原创文章。博客,未经同意,不得转载。

浙公网安备 33010602011771号