题目
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).
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
Sample Output
queue
not sure
impossible
stack
priority queue
题目大意
有一个容器,可能是栈,队列,优先队列,或者都不是;
有多组样例,第一行表示后面跟着n组数据,后面每组数据由两个数字构成第一个数字为1或2,1表示从容器顶存入第二个数据;2表示从容器顶取出第二个数据
要从给出的存取状况判断容器的类别
栈:stack
队列:queue
优先队列:priority queue
可能是三个中的某一个无法判断:not sure
不是三个中的任何一个:impossible
这里有可能出现没有元素了,但是还取了元素出来,那这肯定就不是三个中的任何一个了,需要输出 impossible
需要进行标记或者其他处理,同时后面就需要首先对impossible 的情况进行判断因为这个有两个分支情况(都是我的泪啊!!!!!)
AC代码
#include<cstdio>
#include<stack>
#include<queue>
using namespace std;
int main()
{
int n;
int w1,w2,w3;
int a,b;
int v;
stack<int> x;//定义栈进行模拟
queue<int> y;//定义队列进行模拟
priority_queue<int> z;//定义优先队列进行模拟
while(~scanf("%d",&n))
{
w1=0;
w2=0;
w3=0;
v=0;
for(int i=0; i<n; i++)
{
scanf("%d %d",&a,&b);
if(a==1)//分别向三个模拟的容器顶加入元素
{
x.push(b);
y.push(b);
z.push(b);
}
if(a==2)
{
if(x.empty())//当容器内无元素时用v进行标记(表示不是三个元素的任意一个)
v=1;
else//如果容器顶不是要取的元素则分别标记
{
if(x.top()!=b)
w1=1;
if(y.front()!=b)
w2=1;
if(z.top()!=b)
w3=1;
x.pop();
y.pop();
z.pop();
}
}
}
if((w1+w2+w3)==3||v==1)//当对应三个容器都被标记时或者v被标记时表示不是三个中的任一个(这个必须放在判断的最前面)
printf("impossible\n");
else if((w1+w2+w3)<2)
printf("not sure\n");
else if(w1==0)
printf("stack\n");
else if(w2==0)
printf("queue\n");
else if(w3==0)
printf("priority queue\n");
while(!x.empty())
x.pop();
while(!y.empty())
y.pop();
while(!z.empty())
z.pop();
}
return 0;
}