标签:
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!
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.
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.
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
queue not sure impossible stack priority queue
题意:
判断是哪一种数据结构
题解:
STL <queue> <stack> priority_queue
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #include<queue> 6 #include<stack> 7 #include<map> 8 #include<vector> 9 using namespace std; 10 queue<int> q; 11 stack<int> s; 12 struct ss{ 13 int f; 14 friend bool operator < (ss a, ss b) 15 {return a.f < b.f;} 16 }; 17 priority_queue<ss> pq; 18 int aa,bb; 19 int n; 20 int ans,ans1,ans2,ans3; 21 int cun[1005][2]; 22 int main() 23 { 24 while(scanf("%d",&n)!=EOF) 25 { 26 ans=0;ans1=0;ans2=0;ans3=0; 27 while(q.size()>0) 28 q.pop(); 29 while(s.size()>0) 30 s.pop(); 31 while(pq.size()>0) 32 pq.pop(); 33 memset(cun,0,sizeof(cun)); 34 for(int i=1;i<=n;i++) 35 scanf("%d %d",&cun[i][0],&cun[i][1]); 36 for(int i=1;i<=n;i++) 37 { 38 int aa=cun[i][0]; 39 int bb=cun[i][1]; 40 if(aa==1) 41 { 42 q.push(bb); 43 s.push(bb); 44 struct ss gg; 45 gg.f=bb; 46 pq.push(gg); 47 } 48 else 49 { 50 ans++; 51 if(!q.empty()&&bb==q.front()) 52 { ans1++; q.pop();} 53 if(!s.empty()&&bb==s.top()) 54 {ans2++;s.pop();} 55 if(!pq.empty()&&bb==pq.top().f) 56 {ans3++;pq.pop();} 57 } 58 } 59 //cout<<ans<<" "<<ans1<<" "<<ans2<<" "<<ans3<<" "<<endl; 60 int qu=0,st=0,pri=0; 61 if(ans==ans1) 62 qu=1; 63 if(ans==ans2) 64 st=1; 65 if(ans==ans3) 66 pri=1; 67 if (st + qu + pri > 1) printf("not sure\n"); 68 else if (st) printf("stack\n"); 69 else if (qu) printf("queue\n"); 70 else if (pri) printf("priority queue\n"); 71 else printf("impossible\n"); 72 73 } 74 return 0; 75 } 76 /* 77 3 78 1 1 79 1 2 80 2 1 81 6 82 1 1 83 1 2 84 1 3 85 2 1 86 2 2 87 2 3 88 6 89 1 1 90 1 2 91 1 3 92 2 3 93 2 2 94 2 1 95 */
标签:
原文地址:http://www.cnblogs.com/hsd-/p/5414366.html