uva 11995 I Can Guess the Data Structure!

题目很简单,就是不知道为啥总是runtime error;

后来发现我是先判断的y!=p.front();

应该先判断是否为空;

哎!今天果然不宜A题啊!

代码:

 1 #include<queue>
 2 #include<stack>
 3 #include<cstdio>
 4 using namespace std;
 5 priority_queue<int>pq;
 6 queue<int>q;
 7 stack<int>s;
 8 bool flag[4];
 9 int main()
10 {
11     int n,x,y;
12     while(scanf("%d",&n)!=EOF)
13     {
14         while(!q.empty())q.pop();
15         while(!pq.empty())pq.pop();
16         while(!s.empty())s.pop();
17         flag[0]=1,flag[1]=1,flag[2]=1;
18         while(n--)
19         {
20             scanf("%d%d",&x,&y);
21             if(x==1)
22             {
23                 pq.push(y);
24                 q.push(y);
25                 s.push(y);
26             }
27             else if(x==2)
28             {
29                 if(flag[0]==1){if(pq.empty()||y!=pq.top())flag[0]=0;
30                 else pq.pop();}
31                 if(flag[1]==1){if(q.empty()||y!=q.front())flag[1]=0;
32                 else q.pop();}
33                 if(flag[2]==1){if(s.empty()||y!=s.top())flag[2]=0;
34                 else s.pop();}
35             }
36         }
37         int cnt=0,ans=0;
38         for(int i=0; i<3; i++)
39         {
40             if(flag[i]==1)
41             {
42                 cnt++;
43                 ans+=i;
44             }
45         }
46         if(cnt==0)puts("impossible");
47         else if(cnt>=2)puts("not sure");
48         else
49         {
50             if(ans==0)puts("priority queue");
51             else if(ans==1)puts("queue");
52             else puts("stack");
53         }
54     }
55     return 0;
56 }
View Code

 

posted @ 2013-10-23 22:57  Yours1103  阅读(182)  评论(0编辑  收藏  举报