例题1  uva11995  http://acm.hust.edu.cn/vjudge/problem/18700

 

猜测符合哪种数据结构 , 用stl模拟判断.

 

 1 //#define txtout
 2 //#define debug
 3 #include<bits/stdc++.h>
 4 #define mt(a,b) memset(a,b,sizeof(a))
 5 using namespace std;
 6 typedef long long LL;
 7 const double pi=acos(-1.0);
 8 const double eps=1e-8;
 9 const int inf=0x3f3f3f3f;
10 const int M=1e5+10;
11 int n;
12 int a[M];
13 int b[M];
14 char answer[8][32]={"stack","queue","priority queue","impossible","not sure"};
15 stack<int> s;
16 queue<int> q;
17 priority_queue<int> p;
18 bool checkStack(){
19     while(!s.empty()) s.pop();
20     for(int i=0;i<n;i++){
21         if(a[i]==1){
22             s.push(b[i]);
23             continue;
24         }
25         if(s.empty()) return false;
26         if(s.top()!=b[i]) return false;
27         s.pop();
28     }
29     return true;
30 }
31 bool checkQueue(){
32     while(!q.empty()) q.pop();
33     for(int i=0;i<n;i++){
34         if(a[i]==1){
35             q.push(b[i]);
36             continue;
37         }
38         if(q.empty()) return false;
39         if(q.front()!=b[i]) return false;
40         q.pop();
41     }
42     return true;
43 }
44 bool checkPriority(){
45     while(!p.empty()) p.pop();
46     for(int i=0;i<n;i++){
47         if(a[i]==1){
48             p.push(b[i]);
49             continue;
50         }
51         if(p.empty()) return false;
52         if(p.top()!=b[i]) return false;
53         p.pop();
54     }
55     return true;
56 }
57 int solve(){
58     bool isStack=checkStack();
59     bool isQueue=checkQueue();
60     bool isPriority=checkPriority();
61     if(isStack&&!isQueue&&!isPriority) return 0;
62     if(!isStack&&isQueue&&!isPriority) return 1;
63     if(!isStack&&!isQueue&&isPriority) return 2;
64     if(!isStack&&!isQueue&&!isPriority) return 3;
65     return 4;
66 }
67 int main(){
68     #ifdef txtout
69     freopen("in.txt","r",stdin);
70     freopen("out.txt","w",stdout);
71     #endif // txtout
72     while(~scanf("%d",&n)){
73         for(int i=0;i<n;i++){
74             scanf("%d%d",&a[i],&b[i]);
75         }
76         puts(answer[solve()]);
77     }
78     return 0;
79 }
View Code

相关文章:

  • 2021-08-11
  • 2022-12-23
  • 2021-12-19
  • 2021-05-03
  • 2021-09-21
  • 2021-10-01
  • 2021-05-26
  • 2022-01-12
猜你喜欢
  • 2021-11-27
  • 2021-12-15
  • 2021-10-16
  • 2021-11-19
  • 2022-12-23
  • 2021-12-24
相关资源
相似解决方案