Description
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 (![$1 \le t \le 1000$](http://uva.onlinejudge.org/external/5/540img1.gif)
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
这道题是关于队列的,队列的有关知识:
1.包含头文件<queue>,可以是一个queue,也可以是一堆queue
2.队列是后面插入,前面取出——“先进先出”
3.取队首元素:S.front();进入队列:S.push(i);出队列:S.pop()。
此外这道题要设置两个队列,一个是记录队列序号的顺序,一个是记录各个队列具体的成员的排列顺序。代码如下:
#include<cstdio>
#include<queue>
#include<map>
using namespace std;
const int maxn=1000+5;
int main()
{
int t,count=0;
while(scanf("%d",&t)==1&&t)
{
map<int,int> x;
queue<int> q,q1[maxn];
for(int i=1;i<=t;i++)
{
int n;
scanf("%d",&n);
for(int j=0;j<n;j++)
{
int elem;
scanf("%d",&elem);
x[elem]=i;
}
}
printf("Scenario #%d\n",++count);
char s[15];
while(scanf("%s",s))
{
if(s[0]=='S') break; //注意比较的方式
if(s[0]=='E')
{
int elem,t1;
scanf("%d",&elem);
t1=x[elem];
if(q1[t1].empty()) q.push(t1);
q1[t1].push(elem);
}
if(s[0]=='D')
{
int t1;
t1=q.front();
printf("%d\n",q1[t1].front());
q1[t1].pop();
if(q1[t1].empty()) q.pop();
}
}
printf("\n");
}
return 0;
}
这道题有一个需要注意的地方,只有string类型的可以(S==“STOP”)来比较,而char类型的只能一个一个元素比较或者用strcmp(S,“STOP”)(S1=S2时返回0,S1>S2时大于0,S1<S2时小于0)