HDU-1387-Team queue

Source: Internet
Author: User
Team queue Time 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 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

I have been working on Stack and queue issues over the past few days. I just started out and want to do more!
This is a simulated queue question!
I used the linked list for a long time, because the linked list has not been used for a long time, but it is very troublesome to use the linked list. It is true that I have been entangled for a long time, but it is still written, and the code is not very clear, submit it again and then TLE, alas :-(

Then I looked at other people's code online and found that I still have many shortcomings !! So it's better for new users to look at other people's code! The idea of the code is still very clear. paste it here first and recall it later!

My TLE code (linked list: it is not written using queue in C ++. One is to practice the linked list, and the other 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->da)  && 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", &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 (93 ms ):

#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> 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();printf("%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();}}}


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.