Team Queue
Team Queue |
Queues 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.
Input
The input file will contain one or more test cases. Each test case begins with the number of teams t (
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.
Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.
Output
For 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
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
方法:
数据结构:
哈希表 (链地址法):完成由element查询其所属team号的工作。
struct hash_elem
{
int team; // 元素组号
int num; // 元素
struct hash_elem *next; // 指向下一个同hash值的hash_elem
};
struct hash_elem *hash_arr[N];
二维链表:完成出队和入队工作。
struct team_que_elem
{
int num; //元素
struct team_que_elem *next; // 指向下一个team_que_elem
};
struct team_que // 每一组的元素组成一个新的子队列。
{
int next; // 指向队列中的下一个team_que(数组下标)
struct team_que_elem *head; // 该组 头指针
struct team_que_elem *tail; // 该组的尾指针
};
struct team_que team_que_arr[N]; //队列,其中下标表示组号。
int team_que_head, team_que_tail; 队列头和队列尾的下标,初始都为 0 。
数据结构可保证enqueue和dequeue使用constant time
代码:
#include #include #include #define N 2000 #define H 10000 typedef struct hash_elem hash_elem; struct hash_elem{ int team; int num; struct hash_elem *next; }; typedef struct team_que team_que; typedef struct team_que_elem team_que_elem; struct team_que_elem{ int num; team_que_elem *next; }; struct team_que{ int next; team_que_elem *head; team_que_elem *tail; }; hash_elem *hash_arr[H]; team_que team_que_arr[N]; int team_que_head; int team_que_tail; void hash_init() { int i; for(i = 0; i < H; i++) hash_arr[i] = NULL; } int hash(int n) { return n % H; } void hash_insert(int t, int num) { int n; hash_elem *temp; n = hash(num); temp = (hash_elem *)malloc(sizeof(hash_elem)); temp->team = t; temp->num = num; temp->next = hash_arr[n]; hash_arr[n] = temp; } int hash_query(int num) { int n; hash_elem *temp; n = hash(num); for(temp = hash_arr[n]; temp != NULL; temp = temp->next){ if(temp->num == num) return temp->team; } } void team_que_init() { int i; for(i = 0; i < N; i++){ team_que_arr[i].next = -1; team_que_arr[i].head = NULL; team_que_arr[i].tail = NULL; } } void enqueue(int num) { int n, t; team_que_elem *temp; team_que *tq; temp = (team_que_elem *)malloc(sizeof(team_que_elem)); temp->next = NULL; temp->num = num; n = hash_query(num); tq = team_que_arr + n; if(tq->head == NULL){ team_que_arr[team_que_tail].next = n; team_que_tail = n; tq->head = temp; } else{ tq->tail->next = temp; } tq->tail = temp; } int dequeue() { team_que_elem *temp; int t, n = team_que_arr[team_que_head].next; temp = team_que_arr[n].head; t = temp->num; team_que_arr[n].head = temp->next; free(temp); if(team_que_arr[n].head == NULL){ team_que_arr[team_que_head].next = team_que_arr[n].next; } return t; } int main() { int i, j, t, x, c, num; char com[10]; j = 1; while(scanf("%d", &t) != EOF && t != 0){ hash_init(); team_que_init(); team_que_head = team_que_tail = 0; for(i = 1; i <= t; i++){ scanf("%d", &c); while(c-- > 0){ scanf("%d", &num); hash_insert(i, num); } } printf("Scenario #%d\n", j); j++; while(scanf("%s", com) != EOF){ if(strcmp(com, "ENQUEUE") == 0){ scanf("%d", &x); enqueue(x); } else if(strcmp(com, "DEQUEUE") == 0){ printf("%d\n", dequeue()); } else{ break; } } printf("\n"); } return 0; }