"SGU" 271. Book Pile (double-ended queue emulation)

Source: Internet
Author: User

A stack of books, 2 operations, one operation is to add a book on the heap, the second one will be the first K-book flip

Look at others using splay tree, but you can use the double-ended queue simulation, because the book after the K book position has been determined, so only need to record in the queue header plus book or the end of the book

#include <cstdio> #include <string> #include <algorithm> #include <queue> #include <stack    > #include <cstring> #include <iostream>using namespace Std;int main () {int n,m,k;        while (scanf ("%d%d%d", &n,&m,&k)! = EOF) {deque<string>q1;        deque<string>q2;        int top = 1;            for (int i = 0; i < N; i++) {char str[10];            scanf ("%s", str);            if (Q1.size () >= K) Q2.push_back (String (str));        else Q1.push_back (String (str));            } for (int i = 0; i < M; i++) {char str[50];            scanf ("%s", str);                if (str[0] = = ' A ') {string s = "";                int L = strlen (str);                int OK = 0;                    for (int j = 0; J < L; J + +) {if (str[j] = = ' (') ok = 1;                    else if (str[j] = = ') ') ok = 0;       else if (OK) s + = Str[j];         } if (top) Q1.push_front (s);                else Q1.push_back (s);                        if (Q1.size () > K) {if (top) {string S1 = Q1.back ();                        Q1.pop_back ();                    Q2.push_front (S1);                        } else{string S1 = Q1.front ();                        Q1.pop_front ();                    Q2.push_front (S1);        }}} else top =!top;                if (top) {while (!q1.empty ()) {cout << q1.front () << Endl;            Q1.pop_front ();                }} else{while (!q1.empty ()) {cout << q1.back () << Endl;            Q1.pop_back ();            }} while (!q2.empty ()) {cout << q2.front () << Endl;        Q2.pop_front ();  }  } return 0;} 

"SGU" 271. Book Pile (double-ended queue emulation)

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.