ACM Learning process--uva540 Team Queue (queue, Map:hash)

Source: Internet
Author: User

Description

Team Queue
Team Queue

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.

InputThe input file is contain one or more test cases. Each test case is begins with the number of teams T ( ). 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.

Warning: A test case may contain-200000 (hundred thousand) commands, so the implementation of the team queue should be EF Ficient:both enqueing and dequeuing of an element should only take constant time.

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 was dequeued on a.   Print a blank line after all test case, even after the last one.

Sample Input

101 102 1033 201 202 203ENQUEUE 101ENQUEUE 201ENQUEUE 102ENQUEUE 202ENQUEUE 103ENQUEUE 203DEQUEUEDEQUEUEDEQUEUEDEQUEUED EQUEUEDEQUEUESTOP25 259001 259002 259003 259004 2590056 260001 260002 260003 260004 260005 260006ENQUEUE 259001ENQUEUE 260 001ENQUEUE 259002ENQUEUE 259003ENQUEUE 259004ENQUEUE 259005DEQUEUEDEQUEUEENQUEUE 260002ENQUEUE 260003dequeuedequeuedequeuedequeuestop0

Sample Output

Scenario #1101102103201202203Scenario #2259001259002259003259004259005260001

According to the title, this team queue, only need to consider whether there are people in the queue in the current team,
Therefore, each team can send a representative to queue, and then the team inside the queue at the same time. This represents a practical team foot tag. The team only came out when the team insiders were all out.
However, you need to quickly find out which team each person belongs to, and you need to use a mapping method, where map is used.
The queue inside the team is queued using the chained structure.


Code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm> #include <set> #include <map> #include <vector> #include <queue>    #include <string> #define INF 0x3fffffff#define eps 1e-10using namespace std;struct isqueue{int val; Isqueue *next;};    struct queue{isqueue *top;    Isqueue *rear;        void Init () {top = (Isqueue *) malloc (sizeof (isqueue));        Rear = (Isqueue *) malloc (sizeof (isqueue));        Top->next = rear;    Rear->val =-1;    } void Pop () {top = top->next;    } int Front () {return top->next->val;        } void Push (int k) {Isqueue *v = (Isqueue *) malloc (sizeof (isqueue));        Rear->val = k;        Rear->next = v;        Rear = rear->next;    Rear->val =-1;        } bool Empty () {if (Top->next->val = =-1) return 1; else return0;    }};struct node{int Isin; Queue turn;};    Node P[1005];map <int, int> hash;int t;void Init () {hash.clear ();    int k, V;        for (int i = 0; i < T; ++i) {p[i].isin = 0; P[i].turn.        Init ();        scanf ("%d", &k);            for (int j = 0; j < K; ++j) {scanf ("%d", &v);        HASH[V] = i;    }}}void qt () {char ch[20];    int num;    int x;    Queue <int> q;    for (;;)        {scanf ("%s", ch);            if (ch[0] = = ' S ') {printf ("\ n");        Return            } if (ch[0] = = ' E ') {scanf ("%d", &num);            x = Hash[num];                if (P[x].isin = = 0) {q.push (x); P[x].turn.                Push (num);            p[x].isin++; } else {P[x].turn.                Push (num);            p[x].isin++;         }} else if (ch[0] = = ' D ') {x = Q.front ();   num = P[x].turn.            Front ();            if (P[x].isin = = 1) q.pop (); P[x].turn.            Pop ();            p[x].isin--;        printf ("%d\n", num);    }}}int Main () {//freopen ("test.txt", "R", stdin);    int times = 1;        while (scanf ("%d", &t)! = EOF && T! = 0) {printf ("Scenario #%d\n", times);        Init ();        QT ();    times++; } return 0;}

ACM Learning process--uva540 Team Queue (queue, Map:hash)

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.