題目: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 will contain 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.
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
203app
Scenario #2
259001
259002
259003
259004
259005
260001ui
題目大意:就是先輸入t行,表示t個小隊,其中的數值就是這個小隊的隊員。而後以後再專門組成一個大隊列,這個大隊列和小隊之間沒有關係的,可是在依次排隊的時候,若是這個正準備排隊的成員在以前有隊友在前面,則排到隊友的那一羣的後面,若是沒有隊友則要排到大隊列的隊尾。code
思路:找一個數組還記錄這個小隊是否在大隊裏,且在大隊列用一個二維數組存,其中每一行的首元素表示爲這一行是哪一個小隊的,以後根據這個正要進入大隊列的成員肯定其所在小隊是否已經在大隊列中,若是在則放到這個小隊的那一行的最後,若是不在則在大隊列中的最後表示這個新的小隊的存在,而後再這一個小隊的這一行的第一個元素爲這個成員。而後在出隊伍的時候,依次從大隊列的第一個小隊的第一個成員出,由於這題很是容易超時,因此把千萬避免遍歷,因此要把大隊列的隊首記下來,大隊列的隊尾記錄下來,每一個小隊的第一個元素記下來,每一個小隊的個數記下來。仍是由於時間問題,這題千萬不能移動元素,由於這樣很是耗時,因此要改變的是記錄大隊列隊首和大隊列隊尾的數值,還有改變小隊的隊首與每一個小隊的個數;(也是由於這個緣由,沒辦法用鏈表進行,只能用數組進行);orm
新技巧:對於這種查找耗時很長的隊列問題,用鏈表就相對麻煩不少了,因此這時候就考慮用數組就能夠,並且這題還出現了範圍,因此用數組能夠。(這題若是要改爲鏈表就要把每一個小隊做爲一個隊列而後進行隊尾插入,隊首刪除),不過由於這樣會很是的麻煩,由於要爲每一個存在於大隊列的小隊創建一個鏈表。因此不如用數組方便些(數組會出現內存的問題,這個也要注意)。(因此對於數據結構的問題的處理,用鏈表和數組理論上均可以,但有簡單和方便,還有時間問題的區別,具體選擇哪一個具體分析)(但記住,數組查找方便,插入到中間不方便,刪除也不方便(這題不是直接刪除),隊列問題的數組插入仍是比較簡單。鏈表查找麻煩,由於有很大的可能性須要遍歷,可是插入的時候簡單,只須要改變節點的指向便可,刪除也簡單,尤爲是對於隊列的鏈表處理,只用改變頭節點指向和尾節點指向便可。);three
本身的建議:對於數據結構的問題,最好一上來先考慮用鏈表來進行,畢竟動態分配要靈活的多。當用鏈表實在麻煩,不方便,或者說超時的問題的時候,再去考慮用數組。隊列
代碼:ip
#include<stdio.h> #include<stdlib.h> #include<string.h> int number[1005]; int s_mumber[1000005]; int s_num[5005][1005]; int main() { int t,j,i,num=0,n,ttt,start; char ss[10]; while(scanf("%d",&t)!=EOF&&t!=0) { for(i=0;i<t;i++) { scanf("%d",&n); for(j=0;j<n;j++) { scanf("%d",&ttt); number[j]=0; s_mumber[ttt]=i; } } int times=0; num++;start=0; printf("Scenario #%d\n",num); while(1) { scanf("%s",ss); if(ss[0]=='E') { scanf("%d",&ttt); i=s_mumber[ttt]; if(number[i]==0) { s_num[times][0]=i; s_num[times][1]=1; s_num[times][2]=3; s_num[times][3]=ttt; number[i]=1; times++; } else { for(j=start;j<times;j++) if(i==s_num[j][0]) break; s_num[j][1]++; s_num[j][s_num[j][2]+s_num[j][1]-1]=ttt; } } else if(ss[0]=='S') break; else if(ss[0]=='D') { if(s_num[start][1]==0) { number[s_num[start][0]]=0; start++; printf("%d\n",s_num[start][s_num[start][2]]); s_num[start][2]++; s_num[start][1]--; } else { printf("%d\n",s_num[start][s_num[start][2]]); s_num[start][2]++;s_num[start][1]--; } } } printf("\n"); } return 0; }