ZOJ 2724 Windows Message Queuing (priority queue)

Source: Internet
Author: User
Tags strcmp

Links: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724

Message queue is the basic fundamental of Windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system would add a message to the queue. Meanwhile, the process would do a loop for getting message from the queue according to the priority value if it's not empt Y. Note the less priority value means the higher priority. In this problem, you is asked to simulate the message queue for putting messages to and getting message from the message Queue.

Input

There ' s only one test case in the input. Each command, "GET" or "PUT", which means getting message or putting message. If the command is "PUT", there ' re one string means the message name and both integer means the parameter and priority Follo Wed by. There'll is at the most 60000 command. Note that one message can appear twice or more and if the messages has the same priority, the one comes first would be pro Cessed first. (i.e., FIFO for the same priority.) Process to the End-of-file.

Output

For each "GET" command, the output of the command getting from the message queue with the name and parameter on one line. If There's no message in the queue, output "EMPTY queue!". There ' s no output for "PUT" command.

Sample Input

Getput MSG1 5PUT msg2 4GETGETGET

Sample Output

EMPTY queue!msg2 10MSG1 10EMPTY queue!
Translation:  Message Queuing is the cornerstone of the Windows operating system, for each process, the system maintains a message queue, if a process occurs an event, such as a mouse click, text changes, the system will add a message to the queue, and if the queue is not empty, then, The process will always crawl the message from the queue by the priority value, note that the small value means high priority, in the case, ask you to simulate Message Queuing, put the message into the message queue, or crawl messages from the message queue; Enter a description: There is only one test case, each line is a command, "GET" or "PUT" Represents fetching a message from a message queue or placing a message in a message queue. If it is a "PUT" command followed by a string (representing the message name) and two integers (representing the message's parameters and priority), the number of commands in the case is up to 60,000, note that a command can be repeated multiple times, and if the two commands have the same priority, then the first one to enter the message queue is processed , always processing to the end of the file; Output description: For each "GET" instruction, directly output the name and parameters of the message he crawled on one line, if the message queue is empty, then directly output "empty queue!", for "PUT" instruction, do not need to output what;
Problem-Solving ideas: The topic is to simulate Windows processing message queue, it makes sense, the main problem encountered in solving problems is time-out errors, according to the characteristics of the subject, it is advisable to use priority queue container to achieve; In addition, the priority queue container is not available, you must use scanf and printf input output, otherwise it will time out, Because of the large amount of data in the subject, as many as 60000 lines of string need to be processed, input and output;
Code:
#include <iostream> #include <cstdio> #include <queue> #include <cstdlib> #include <cstring    >using namespace Std;struct Message {//Do not use typedef here, because overloaded operator parameter type message is not recognized; Char name[200];    int data, priority;    BOOL operator < (const Message a) const//Overload < operator custom sorting criteria; {return a.priority < priority;    }};int Main () {priority_queue <Message> V;//define a priority queue object; char command[200];        while (~SCANF ("%s", command)) {//Enter commands;        Message message;  if (strcmp (command, "GET") = = 0) {if (v.size () = = 0) {//queue is empty; printf ("Empty queue!\n");            Use cout<< "EMPTY QUEUE" <<endl; will not time out; }else {printf ("%s%d\n", V.top (). Name, V.top (). data); Use Cout<<v.top ().    Name<<v.top (). data will time out; V.pop ();            Outgoing queue operation, the current message is cleared; }}else if (strcmp (command, "PUT") = = 0) {scanf ("%s%d%d", message.) Name, &message.data, &message.priority);   V.push (message); into row;}} return 0;}



Related Article

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.