"Analog + data Structure" UVA 11995 I Can Guess the Data structure! topic
Gives a series of operations, including operands and opcode, to determine the type of data structure (stack, queue, priority queue) that matches the return value of this series of operations
–
Talk about the idea.
- take these three kinds of data structures and simulate them.
"Note" Stack top stack.top ()
Team First Queue.front ()
Heap Top Priority_queue.top ()
Heap is also called priority queue heap = = Priority_queue
Reference Code
#include <bits/stdc++.h>using namespace STD;Const int_max =1e3+Ten;intN,op[_max],x[_max];//operation number and operation code Stack<int>Stk Queue<int>q;priority_queue<int>pq;BOOLIsstack () { while(!stk.empty ()) Stk.pop (); for(inti =0; I < n; + + i) {if(Op[i] = =1) Stk.push (X[i]);Else{if(Stk.empty ())return false;intv = stk.top (); Stk.pop ();//Top of Stack if(v! = X[i])return false; } }return true;}BOOLIsqueue () { while(!q.empty ()) Q.pop (); for(inti =0; I < n; + + i) {if(Op[i] = =1) Q.push (X[i]);Else{if(Q.empty ())return false;intv = Q.front (); Q.pop ();//Team first front if(v! = X[i])return false; } }return true;}BOOLIspriority () { while(!pq.empty ()) Pq.pop (); for(inti =0; I < n; + + i) {if(Op[i] = =1) Pq.push (X[i]);Else{if(Pq.empty ())return false;intv = pq.top ();p q.pop ();//Heap Top if(v! = X[i])return false; } }return true;}intMain () {#ifndef Online_judgeFreopen ("Input.txt","R", stdin);#endif //Online_judge while(scanf("%d", &n) = =1){ for(inti =0; I < n; + + i)scanf("%d%d", op+i,x+i);BOOLOk1 = Isstack (), Ok2 = Isqueue (), OK3 = Ispriority ();if(OK1&&!OK2&&!OK3) {puts("Stack");Continue;}if(!OK1&&OK2&&!OK3) {puts("Queue");Continue;}if(!OK1&&!OK2&&OK3) {puts("Priority queue");Continue;}if(!OK1&&!OK2&&!OK3) {puts("Impossible");Continue;}puts("not sure"); }return 0;}
- Bold
Ctrl + B
- Italic Body
Ctrl + I
- Reference
Ctrl + Q
- Insert Link
Ctrl + L
- Inserting code
Ctrl + K
- Insert Picture
Ctrl + G
- Promote title
Ctrl + H
- Ordered list
Ctrl + O
- Unordered list
Ctrl + U
- Line
Ctrl + R
- Revoke
Ctrl + Z
- Redo
Ctrl + Y
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Analog + data Structure" UVA 11995 I Can Guess the Data structure!