#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include <map>
using namespace std;
map<int,int> team;
typedef struct lst{
int data;
struct lst *next;
}lst;
map<int,lst*> pos;
lst *head=(lst*)malloc(sizeof(lst));
lst *tail=head;
void gointo(int x)
{
if(pos.count(team[x]))
{
lst *nod=(lst*)malloc(sizeof(lst));
nod->data=x;
nod->next=pos[team[x]]->next;
if(pos[team[x]]->next==NULL) tail=nod;
pos[team[x]]->next=nod;
pos[team[x]]=nod;
}
else
{
lst *nod=(lst*)malloc(sizeof(lst));
tail->next=nod;
nod->data=x;
tail=nod;
tail->next=NULL;
pos[team[x]]=tail;
}
}
void goout()
{
int x= head->next->data;
cout<<x<<endl;
lst *p=head->next->next;
if(p==NULL) tail=head;
free(head->next);
head->next=p;
}
int main()
{
int t,cnt=0;
while(cin>>t,t!=0)
{
cnt++;
cout<<"Scenario #"<<cnt<<endl;
team.clear();
pos.clear();
tail=head;
tail->next=NULL;
for(int i=1;i<=t;i++)
{
int n;
cin>>n;
for(int j=1;j<=n;j++)
{
int tem;
cin>>tem;
team[tem]=i;
}
}
string s;
while(cin>>s,s!="STOP")
{
if(s[0]=='E')
{
int x;
cin>>x;
gointo(x);
}
if(s[0]=='D') goout();
}
}
free(head);
return 0;
}
思路是 手写链表模拟队列,用map实现查找