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
Scenario #1 101 102 103 201 202 203 Scenario #2 259001 259002 259003 259004 259005 260001
#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; }
#include <cstdio> #include <cstring> #include <queue> using namespace std; #define MAX_RANK 1000000 #define MAX_QUE 1000 #define MAX_N 1000 #define CMD_CHAR 30 int 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) { // init init(); // enter team int 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 commands printf("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(); } } }
原文地址:http://blog.csdn.net/u014355480/article/details/40651897