题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3146
这个题目的意思很简单,给你一些push和pop操作,然后根据操作的结果来猜数据结构。
就三种,
stack
queue
priority queue
正好用STL就可以模拟。
但是模拟的时候要注意,在pop的时候,STL中的可能已经为空了。所以要加个判断进去。
下面看我的代码:
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
using namespace std;
stack<int> a;
queue<int> b;
priority_queue<int> c;
int main()
{
int n;
while(~scanf("%d",&n))
{
int x,y;
while(!a.empty())
a.pop();
while(!b.empty())
b.pop();
while(!c.empty())
c.pop();
bool aa,bb,cc;
aa=bb=cc=true;
while(n--)
{
scanf("%d%d",&x,&y);
if(x==1)
{
a.push(y);
b.push(y);
c.push(y);
}
else
{
if(!a.empty())
{
int aaa = a.top();
a.pop();
if(aaa!=y)
aa=false;
}
else
aa=false;
if(!b.empty())
{
int bbb = b.front();
b.pop();
if(bbb!=y)
bb=false;
}
else
bb=false;
if(!c.empty())
{
int ccc = c.top();
c.pop();
if(ccc!=y)
cc=false;
}
else
cc=false;
}
}
if((aa&&bb) || (aa&&cc) || (bb&&cc))
printf("not sure\n");
else if(aa)
printf("stack\n");
else if(bb)
printf("queue\n");
else if(cc)
printf("priority queue\n");
else
printf("impossible\n");
}
return 0;
}