Ultraviolet A 540 team queue

Source: Internet
Author: User

QueuesAndPriority QueuesAre data structures which are known to most computer scientists.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
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 itsTeammates(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 in one or more test cases. Each test case begins with the number of teams T ().
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:

  • EnqueueX-Enter ElementXInto 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 in up to 200000 (two hundred thousand) commands, so the implementation of the team queue shocould be efficient: Both enqueing and dequeuing of an element shocould 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 eachDequeueCommand, 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

23 101 102 1033 201 202 203ENQUEUE 101ENQUEUE 201ENQUEUE 102ENQUEUE 202ENQUEUE 103ENQUEUE 203DEQUEUEDEQUEUEDEQUEUEDEQUEUEDEQUEUEDEQUEUESTOP25 259001 259002 259003 259004 2590056 260001 260002 260003 260004 260005 260006ENQUEUE 259001ENQUEUE 260001ENQUEUE 259002ENQUEUE 259003ENQUEUE 259004ENQUEUE 259005DEQUEUEDEQUEUEENQUEUE 260002ENQUEUE 260003DEQUEUEDEQUEUEDEQUEUEDEQUEUESTOP0

Sample output

Scenario #1101102103201202203Scenario #2259001259002259003259004259005260001

A really tough question. Although the question is short, it is hard to understand. When you join the team, you can see which queue you belong to is added to the back of which queue, the team goes out of the queue according to the order of the queue.

Language: C ++

Code:

#include<iostream>#include<queue>#include<vector>#include<map>#include<cstdio>#include<cstring>using namespace std;int main(){    //freopen("in.txt","r",stdin);    int quenum;    int cas=1;    while(scanf("%d",&quenum))    {        //cout<<quenum<<endl;        int n,num;        bool inque[1000]= {false};        map<int,int>mp;        if(quenum==0)break;        vector<queue<int> >v;        queue<int>ans;        for(int i=0; i<quenum; i++)        {            v.push_back(queue<int>());            scanf("%d",&n);            for(int j=0; j<n; j++)            {                scanf("%d",&num);                mp[num]=i;            }        }        char s[10];        printf("Scenario #%d\n",cas++);        while(scanf("%s",s))        {            if(strcmp(s,"STOP")==0)break;            if(strcmp(s,"ENQUEUE")==0)            {                scanf("%d",&num);                v[mp[num]].push(num);                if(!inque[mp[num]])                {                    ans.push(mp[num]);                    inque[mp[num]]=true;                }            }            else            {                printf("%d\n",v[ans.front()].front());                v[ans.front()].pop();                if(v[ans.front()].empty())                {                    inque[ans.front()]=false;                    ans.pop();                }            }        }        puts("");    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.