对三种数据结构分别模拟一下即可,注意2操作可能多于1操作。
1 #include <iostream> 2 #include <stack> 3 #include <queue> 4 using namespace std; 5 6 const int N = 1000; 7 int n, cnt; 8 int op[N]; 9 int num[N]; 10 stack<int> s; 11 queue<int> q; 12 priority_queue<int> p; 13 bool flag1, flag2, flag3; 14 15 void init() 16 { 17 cnt = 3; 18 flag1 = flag2 = flag3 = true; 19 while ( !s.empty() ) s.pop(); 20 while ( !q.empty() ) q.pop(); 21 while ( !p.empty() ) p.pop(); 22 } 23 24 int main () 25 { 26 while ( cin >> n ) 27 { 28 bool bl = true; 29 int c1 = 0, c2 = 0; 30 for ( int i = 0; i < n; i++ ) 31 { 32 cin >> op[i] >> num[i]; 33 if ( op[i] == 1 ) c1++; 34 else c2++; 35 if ( c2 > c1 ) bl = false; 36 } 37 if ( !bl ) 38 { 39 cout << "impossible" << endl; 40 continue; 41 } 42 init(); 43 for ( int i = 0; i < n; i++ ) 44 { 45 if ( op[i] == 1 ) 46 { 47 s.push(num[i]); 48 } 49 else 50 { 51 int tmp = s.top(); 52 s.pop(); 53 if ( num[i] != tmp ) 54 { 55 flag1 = false; 56 cnt--; 57 break; 58 } 59 } 60 } 61 for ( int i = 0; i < n; i++ ) 62 { 63 if ( op[i] == 1 ) 64 { 65 q.push(num[i]); 66 } 67 else 68 { 69 int tmp = q.front(); 70 q.pop(); 71 if ( num[i] != tmp ) 72 { 73 flag2 = false; 74 cnt--; 75 break; 76 } 77 } 78 } 79 for ( int i = 0; i < n; i++ ) 80 { 81 if ( op[i] == 1 ) 82 { 83 p.push(num[i]); 84 } 85 else 86 { 87 int tmp = p.top(); 88 p.pop(); 89 if ( num[i] != tmp ) 90 { 91 flag3 = false; 92 cnt--; 93 break; 94 } 95 } 96 } 97 if ( cnt > 1 ) 98 { 99 cout << "not sure" << endl; 100 } 101 else if ( cnt == 1 ) 102 { 103 if ( flag1 ) cout << "stack" << endl; 104 if ( flag2 ) cout << "queue" << endl; 105 if ( flag3 ) cout << "priority queue" << endl; 106 } 107 else 108 { 109 cout << "impossible" << endl; 110 } 111 } 112 return 0; 113 }