(Hdu step 8.1.8) Team Queue (when a person queues, if there is a person he knows before, then he can directly rank behind the person he knows, or else he will be at the end of the Team. In this case), javaqueue

Source: Internet
Author: User

(Hdu step 8.1.8) Team Queue (when a person queues, if there is a person he knows before, then he can directly rank behind the person he knows, or else he will be at the end of the Team. In this case), javaqueue

Question:

Team Queue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 95 Accepted Submission (s): 48
 
Problem DescriptionQueues and Priority Queues are data structures which are known to most computer scientists. the Team Queue, however, is not so well known, though it occurs often in everyday life. at lunch time the queue in front of the Mensa is a team queue, for example.
In a team queue each element belongs to a team. if an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. if yes, it enters the queue right behind them. if not, it enters the queue at the tail and becomes the new last element (bad luck ). dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.

Your task is to write a program that simulates such a team queue.

 
InputThe input will contain in one or more test cases. each test case begins with the number of teams t (1 <= t <= 1000 ). then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. elements are integers in the range 0-999999. A team may consist of up to 1000 elements.

Finally, a list of commands follows. There are three different kinds of commands:

ENQUEUE x-enter element x into the team queue
DEQUEUE-process the first element and remove it from the queue
STOP-end of test case
The input will be terminated by a value of 0 for t.

 
OutputFor each test case, first print a line saying "Scenario # k", where k is the number of the test case. then, for each DEQUEUE command, print the element which is dequeued on a single line. print a blank line after each test case, even after the last one.
 
Sample Input
23 101 102 1033 201 202 203ENQUEUE 101ENQUEUE 201ENQUEUE 102ENQUEUE 202ENQUEUE 103ENQUEUE 203DEQUEUEDEQUEUEDEQUEUEDEQUEUEDEQUEUEDEQUEUESTOP25 259001 259002 259003 259004 2590056 260001 260002 260003 260004 260005 260006ENQUEUE 259001ENQUEUE 260001ENQUEUE 259002ENQUEUE 259003ENQUEUE 259004ENQUEUE 259005DEQUEUEDEQUEUEENQUEUE 260002ENQUEUE 260003DEQUEUEDEQUEUEDEQUEUEDEQUEUESTOP0
 
Sample Output
Scenario #1101102103201202203Scenario #2259001259002259003259004259005260001
 
 
SourceUniversity of Ulm Local Contest 1998
RecommendEddy


Question:

There are many people, each of whom belongs to a team. Now everyone is in a queue (it doesn't matter to the Team) and follows the enqueue or dequeue command. However, in the enqueue scenario, if there are acquaintances in front of the team (people in the same team can be inserted behind that person)


It is equivalent to arranging a group for meals in the canteen. If you have a classmate in front, you can jump in to the back. If you do not have a group, you can arrange it at the end.



Question Analysis:

Use a queue.



The Code is as follows:

/** H. cpp ** Created on: March 24, 2015 * Author: administrator */# include <iostream> # include <cstdio> # include <queue> # include <map> # include <cstring> using namespace std; const int maxn = 1001; bool visited [maxn]; int main () {int cnt = 1; int n; while (scanf ("% d", & n )! = EOF, n) {memset (visited, false, sizeof (visited); queue <int> q [maxn]; // each team. the number of each individual is stored in the queue <int> que; // total queue. Map <int, int> team; // team [t] = I. Indicates that the person numbered t belongs to the int I of Team I; for (I = 0; I <n; ++ I) {int m; scanf ("% d", & m); while (m --) {int teamnums; scanf ("% d", & teamnums); team [teamnums] = I; // initialize the team for each serial number ...}} printf ("Scenario # % d \ n", cnt ++); string str; while (cin> str) {// enter the command if (str = "STOP") continuously {// if the current command is STOPbreak; // then jump out of loop} else if (str = "ENQUEUE") {// if the current command is ENQUEUEint t; scanf ("% d", & t ); q [team [t]. push (t); // insert t to the corresponding team. if (visited [team [t] = false) {// if this team has not been accessed (not in the queue) que. push (team [t]); // Insert the serial number of the team to the total queue visited [team [t] = true; // mark this team as already accessed} else if (str = "DEQUEUE") {// if the current command is DEQUEUEprintf ("% d \ n ", q [que. front ()]. front (); // print the element q [que. front ()]. pop (); // leave the opponent's element in the team if (q [que. front ()]. empty () = true) {// if the team is empty, visited [que. front ()] = false; // re-mark this team as an access que. pop (); // remove the team number from the total queue ..}}} printf ("\ n");} return 0 ;}






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.