Example 3.1 Guess data structure UVa11995

Source: Internet
Author: User

1. Title Narrative Description: Click to open the link

2. How to solve the problem: according to a possible data structure, the alternative answer is "stack, queue." Priority queue ". The results may also be either not or uncertain.

There are already three data structures in the STL, so it is possible to directly simulate the test instructions and infer whether the output is appropriate. Note: You should infer if it is empty when it pops up.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std;stack<int>s;queue<int>q;  Priority_queue<int>p;char st[][20] = {' Impossible ', ' priority queue ', ' queue ', ' not sure ', ' stack ', ' not sure ', ' not ' Sure "," not sure "};void clear () {while (!s.empty ()) S.pop (), while (!q.empty ()) Q.pop (), while (!p.empty ()) P.pop ();} void Add (int x) {s.push (x); Q.push (x);p. push (x);} void Pop () {if (!s.empty ()) S.pop (), if (!q.empty ()) Q.pop (), if (!p.empty ()) P.pop ();} int main () {//freopen ("T.txt", "R", stdin), int n;while (~SCANF ("%d", &n)) {clear (); int Ok1, ok2, Ok3;ok1 = Ok2 = OK3 = 1 ; for (int i = 0; i < n; i++) {int A, b;cin >> a >> b;if (a= = 1) add (b);//Unified Join ELSE{IF (S.empty () | | | s.top ()! = b) Ok1 = 0;if (Q.empty () | | Q.front ()! = b) Ok2 = 0;if (P.empty () | | | p.to P ()! = b) Ok3 = 0;pop ();//Unified eject}}int x = (ok1 << 2) | (ok2 << 1) | ok3;//encoding, easy to output results printf ("%s\n", St[x]);} return 0;}

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Example 3.1 Guess data structure UVa11995

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.