I Can Guess the Data Structure!
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). The size of input file does not exceed 1MB.
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
Output for the Sample Input
queue not sure impossible stack priority queue
Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!
【题目大意】
给你一组输入的数据与输出的数据,对比问符合哪种数据结构(stack,queue,priority_queue)。
【解题思路】
按照三种数据结构模拟输入输出,最后对比即可。
【解题代码】
#include <cstdio>
#include <cstring>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=10010;
int n;
int a[maxn],b[maxn];
int sta[maxn],que[maxn],pqu[maxn];
bool flag[3];
int main()
{
while(~scanf("%d",&n))
{
stack <int> st;
queue <int> qu;
priority_queue <int> pq;
memset(flag,0,sizeof(flag));
int m=-1,kase=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&m,&b[i]);
if(m==1)
{
st.push(b[i]);
qu.push(b[i]);
pq.push(b[i]);
}
else if(m==2)
{
a[kase]=b[i];
if(!flag[0]&&!st.empty())//要注意判断是不是弹出多了
{
sta[kase]=st.top();
st.pop();
if(sta[kase]!=a[kase]) flag[0]=1;
}
else flag[0]=1;
if(!flag[2]&&!pq.empty())//要注意判断是不是弹出多了
{
pqu[kase]=pq.top();
pq.pop();
if(pqu[kase]!=a[kase]) flag[2]=1;
}
else flag[2]=1;
if(!flag[1]&&!qu.empty())//要注意判断是不是弹出多了
{
que[kase]=qu.front();
qu.pop();
if(que[kase]!=a[kase]) flag[1]=1;
}
else flag[1]=1;
kase++;
}
}
// for(int i=0;i<kase;i++)
// printf("a %d st %d qu %d pq %d \n",a[i],sta[i],que[i],pqu[i]);
// 打印
if(!flag[0]&&flag[1]&&flag[2])
printf("stack\n");
else if(!flag[1]&&flag[0]&&flag[2])
printf("queue\n");
else if(!flag[2]&&flag[0]&&flag[1])
printf("priority queue\n");
else if(flag[0]&&flag[1]&&flag[2])
printf("impossible\n");
else printf("not sure\n");
}
return 0;
}
【收获与反思】
一开始一直RE,应该是因为数据里可能2的数量>1的数量,导致栈/队列/优先队列为空时还弹出,导致了RE,所以一定要做好判断检测。