HDU 1387 Team Queue

Source: Internet
Author: User
Tags data structures
Team Queue

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1410 Accepted Submission (s): 475


Problem Description
Queues and priority Queues is data structures which is known to most computer scientists. The Team Queue, however, isn't so well known, though it occurs often in everyday life. At lunch time the "queue in front of the Mensa are 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) is 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 do like in normal queues:elements be processed from head to tail in the order they appear in the team que Ue.

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



Input
The input would contain one or more test cases. Each test case is 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 them Selves. Elements is integers in the range 0-999999. A team may consist of up to elements.

Finally, a list of commands follows. There is 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 is terminated by a value of 0 for T.



Output
For each test case, first print a line saying "Scenario #k", where K was the number of the the test case. Then, for each DEQUEUE command, print the element which was dequeued on a. Print a blank line after all test case, even after the last one.


Sample Input
2
3 101 102 103
3 201 202 203
ENQUEUE 101
ENQUEUE 201
ENQUEUE 102
ENQUEUE 202
ENQUEUE 103
ENQUEUE 203
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
2
5 259001 259002 259003 259004 259005
6 260001 260002 260003 260004 260005 260006
ENQUEUE 259001
ENQUEUE 260001
ENQUEUE 259002
ENQUEUE 259003
ENQUEUE 259004
ENQUEUE 259005
DEQUEUE
DEQUEUE
ENQUEUE 260002
ENQUEUE 260003
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0


Sample Output
Scenario #1
101
102
103
201
202
203

Scenario #2
259001
259002
259003
259004
259005

260001


K Teams queue up, if the person who knows is in the back of the acquaintance, (people here know that they are in the same team), if there is no need to start at the end of the queue of pain,

First WA: The number of the team should be stored in Q, not the number of the players .... Submit WA

Second time: The declaration queue Q2 is placed in the program execution, and each time the new data is entered, the new queue is regenerated because the queue does not have a function interface to empty the queue elements, and the collection map has the ability to empty the array elements. After correction, submit WA.

The third time: the code and the AC Code 1.1 control clones, and finally found that the output format is wrong ... Halo, the case should be starting from 1, not starting from 0 .... Submit AC


#include <iostream> #include <cstdio> #include <map> #include <queue> #include <fstream>
using namespace Std;

Map<int,int> team;
    int main (void) {//FStream cin ("F.txt");
    int t,o=1;
       while (cin>>t,t) {//Q.clear ();
        Q2.clear ();
        o++;
         for (int i=0;i<t;i++) {int m;
        int x;
        cin>>m;
        for (int j=0;j<m;j++) {cin>>x;
        Team[x]=i;
        }} queue<int> q,q2[1100];
          cout<< "Scenario #" <<o++<<endl;
           char c;
        cin>>c;
        while (1) {string S;
        cin>>s;
        if (s== "STOP") break;
            else if (s== "ENQUEUE") {int x;
            cin>>x;
            int k=team[x];
            if (Q2[k].empty ()) Q.push (k);
        Q2[k].push (x);
      } else if (s== "DEQUEUE")  {int K=q.front ();
            Cout<<q2[k].front () <<endl;
            Q2[k].pop ();
        if (Q2[k].empty ()) Q.pop ();
        }} cout<<endl;
    Team.clear ();
} 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.