Dual-end queue deque sdut oj bidirectional queue

Source: Internet
Author: User
Bidirectional queueTime Limit: 1000 MS Memory limit: 65536 KDescription

Think about two-way linked list ...... The definition of a two-way queue is similar. That is to say, the end of a queue is also the first. Both ends can be used to join the queue.
Please output the status of the last queue for a series of operations;
Command format:
Lin x represents an integer, and the Command represents the team-up operations on the left;
Rin x indicates entering the team on the right;
ROUT
LOUT indicates a team-out operation;

Input

The first row contains an integer M (M <= 10000), indicating that M operations exist;
Each line of the following M line contains a command;
The command may be invalid. For invalid commands, process them in the output;

Output

The first line of the output contains the status of the queue after M operations, which is output from left to right, separated by spaces;
The following lines process invalid commands (if any );
For invalid commands, output a line of X ERROR
X indicates the number of commands;

Sample input
8LIN 5RIN 6LIN 3 LOUTROUTROUTROUTLIN 3
Sample output
37 ERROR

Typical operation simulation questions for dual-end queues! I read the wrong question at the beginning, WA once!
Note: output the data in the queue after processing the m command, and then output the wrong command information in sequence!
# Include <iostream> # include <string> # include <stdio. h> # include <string. h> # include <map> # include <stack> # include <deque> // double-end queue # include <algorithm> # include <ctype. h> using namespace std; int a [20000], e; int main () {int m; cin> m; int I, j, dd; string s; deque <int> q; deque <int>: iterator it; for (I = 1; I <= m; I ++) {cin> s; if (s = "LIN") {cin> dd; q. push_front (dd);} else if (s = "RIN") {cin> dd; q. push_ba Ck (dd);} else if (s = "LOUT") {if (q. empty () {a [e ++] = I;} else {q. pop_front () ;}} else if (s = "ROUT") {if (q. empty () {a [e ++] = I;} else {q. pop_back () ;}} int flag = 0; for (it = q. begin (); it! = Q. end (); it ++) {if (flag = 0) {printf ("% d", * it); flag = 1 ;} else if (flag = 1) {printf ("% d", * it) ;}} if (flag = 1) {printf ("\ n "); // If flag = 1, it indicates that there is data in the queue in the final state. After the data is output, it will output a line break! If flag = 0, there is no need to wrap it! } For (j = 0; j <e; j ++) {printf ("% d ERROR \ n", a [j]);} return 0 ;}

 

 

Dual-end queue deque sdut oj bidirectional queue

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.