Hdu-1387-team Queue

Source: Internet
Author: User
Tags strcmp

Team QueueTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1259 Accepted Submission (s): 430


Problem Descriptionqueues 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 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.


Outputfor 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
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

Sourceuniversity of Ulm Local Contest 1998

These days are doing stacks and queue problems. Just getting started. Want to do something more!
This is a problem with a simulated queue!
I used to write the list before. It's been a long time since the list was useless. But the problem with the list is very troublesome, indeed. Tangled me for a long time, but still write out, the code is not very clear, submitted again and then Tle, alas:-(

Then read the code on the Internet to see others, found that they have a lot of shortcomings!

! So the novice still have to look at other people's Code AH! That code idea is still very clear, first posted here, and then to review the review!

My tle code (linked list: No use of C + + queue write, one is to practice the list.) The second is to simulate the process):

#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < iostream> #include <queue> #include <stack>using namespace std;typedef struct Que{int da;struct que* next;} *queu, node;struct fun{int da;int te;} Te_me[1000000];int me_num;int Len (Queu q) {int len=0;while (q->next) {q = q->next;len++;} return Len;} int query (int a) {for (int i=0; i<me_num; i++) if (a = = Te_me[i].da) return te_me[i].te;} int entry_q (int A, queu q, int len) {int team = query (a), for (int i=0; i < Len; i++) {if (Team = = Query (Q-&GT;DA) &&am P Team! = Query (Q->next->da)) {Queu p = (node*) malloc (sizeof (node)); P->next=null;p->da = A;p->next = q-> Next;q->next = P;return 1;} else q = q->next;} return 0;}  int main () {int T, Count=0;while (scanf ("%d", &t), t) {count++;int n; me_num=0;for (int k = 1; k <= T; k++) {scanf ("%d", &AMP;N); int M;while (n--) {scanf ("%d", &m); te_me[me_num].da = M;te_me[me_num].te = k;me_num++;}} printf ("ScenArio #%d\n ", count); Queu q = (node*) malloc (sizeof (node)); Q->next=null;queu front=q, rear=q, P;char fun[10]; int Elem;while (scanf ("%s", fun)!=eof) {if (!STRCMP (fun, "ENQUEUE")) {scanf ("%d", &elem); if (front = = rear) {rear-> da = Elem;p = (node*) malloc (sizeof (node)); P->next=null;rear->next = p; Rear = P;} else if (! Entry_q (Elem, Front, Len (front))) {Rear->da = Elem;p = (node*) malloc (sizeof (node)); P->next=null;rear->next = P ; Rear = P; }}else if (!strcmp (fun, "DEQUEUE")) {if (Front->da) printf ("%d\n", Front->da); Queu Fe=front;front = fe->next; Free (FE);} else if (!strcmp (fun, "STOP")) {break;}} printf ("\ n");}  return 0;}



AC Code (93MS):

#include <cstdio> #include <cstring> #include <queue>using namespace std; #defineMAX_RANK 1000000# Define Max_que 1000#define max_n 1000#define cmd_char 30int team[max_rank];queue<int> que[max_que];queue<int& Gt Bigque;void init (); int main () {int cases = 1;int Teamm;while (scanf ("%d", &teamm) = = 1 && teamm) {//Initinit ()  ;//Enter Teamint N;memset (team, 0, sizeof (team)), for (int team_no = 0; scanf ("%d", &n) = = 1; team_no++) {for (int i = 0; I < n; i++) {int num;scanf ("%d%*c", &num); Team[num] = Team_no;}} Read commandsprintf ("Scenario #%d\n", cases++), while (true) {char cmd[cmd_char];scanf ("%s", cmd), if (strcmp (cmd, " ENQUEUE ") = = 0) {int num;scanf ("%d%*c ", &num), if (Que[team[num]].empty ()) {Bigque.push (Team[num]);} Que[team[num]].push (num);} else if (strcmp (cmd, "DEQUEUE") = = 0) {int whitch_team = Bigque.front ();p rintf ("%d\n", Que[whitch_team].front ()); que[ Whitch_team].pop (); if (Que[whitch_team].empty ()) {Bigque.pop ();}} else {printf("\ n"); break;}} return 0;} void Init () {while (!bigque.empty ()) {Bigque.pop ();} for (int i = 0; i < Max_que, i++) {while (!que[i].empty ()) {Que[i].pop ();}}}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Hdu-1387-team 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.