"Analog + data Structure" UVA 11995 I Can Guess the Data structure!

Source: Internet
Author: User

"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&AMP;&AMP;!OK2&AMP;&AMP;!OK3) {puts("Stack");Continue;}if(!OK1&AMP;&AMP;OK2&AMP;&AMP;!OK3) {puts("Queue");Continue;}if(!OK1&AMP;&AMP;!OK2&AMP;&AMP;OK3) {puts("Priority queue");Continue;}if(!OK1&AMP;&AMP;!OK2&AMP;&AMP;!OK3) {puts("Impossible");Continue;}puts("not sure"); }return 0;}
    • Bold Ctrl + B
    • Italic Body Ctrl + I
    • ReferenceCtrl + Q
    • Insert LinkCtrl + L
    • Inserting codeCtrl + K
    • Insert PictureCtrl + G
    • Promote titleCtrl + H
    • Ordered listCtrl + O
    • Unordered listCtrl + U
    • LineCtrl + R
    • RevokeCtrl + Z
    • RedoCtrl + 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.