Google中国2015校园招聘笔试Round D APAC Test Problem C. Sort a scrambled itinerary

原创 2014年11月14日 11:44:02

Problem

Once upon a day, Mary bought a one-way ticket from somewhere to somewhere with some flight transfers.

For example: SFO->DFW DFW->JFK JFK->MIA MIA->ORD.

Obviously, transfer flights at a city twice or more doesn't make any sense. So Mary will not do that.

Unfortunately, after she received the tickets, she messed up the tickets and she forgot the order of the ticket.

Help Mary rearrange the tickets to make the tickets in correct order.

Input

The first line contains the number of test cases T, after which T cases follow.
For each case, it starts with an integer N. There are N flight tickets follow.
Each of the next 2 lines contains the source and destination of a flight ticket.

Output

For each test case, output one line containing "Case #x: itinerary", where x is the test case number (starting from 1) and itinerary is sorted list of flight tickets which represents the actual itinerary. Each flight segment in the itinerary should be outputted as pair of source-destination airport codes.

Limits

1 ≤ T ≤ 100.
For each case, the input tickets are messed up from an entire itinerary bought by Mary. In other words, it's ensured can be recovered to a valid itinerary.

Small dataset

1 ≤ N ≤ 100.

Large dataset

1 ≤ N ≤ 104.

(The segment for second case in sample can be seen as below) MIA-ORD, DFW-JFK, SFO-DFW, JFK-MIA

Sample


Input 
 

Output 
 
2
1
SFO
DFW
4
MIA
ORD
DFW
JFK
SFO
DFW
JFK
MIA

Case #1: SFO-DFW
Case #2: SFO-DFW DFW-JFK JFK-MIA MIA-ORD

类型:其他  难度:1.5

题意:给出一条航线上的若干机票,即A-B,B-C,C-D等,但是顺序被打乱了,将机票按顺序排好,数据保证给出的机票是合法的。

分析:对每个机票,用map[start] = end 记录每一段的起点和终点。接下来需要找到第一个起点,我的方法是用一个set记录所有终点,然后遍历map的key,若当前key没有出现在set里,那么找到了第一个起点,由于数据合法,接下来只要按顺序找即可。


代码如下:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <map>
using namespace std;

int n;
map<string,string> mp;
set<string> en;

int main() {
    freopen("C-large.in", "r", stdin);
    freopen("C-large.out", "w", stdout);

    int t;
    scanf("%d",&t);

    for(int cnt=1; cnt<=t; ++cnt)
    {
        mp.clear();
        en.clear();
        scanf("%d",&n);
        for(int i=0; i<n; ++i)
        {
            char a[30],b[30];
            scanf("%s%s",a,b);
            string x = a, y = b;
            
            mp[x] = y;
            en.insert(y);
        }
        string st,des;
        for(auto p=mp.begin(); p!=mp.end(); ++p)
        {
            if(en.find(p->first)==en.end())
            {
                st = p->first;
                break;
            }
        }
        
        printf("Case #%d:",cnt);
        for(; mp.find(st)!=mp.end(); st = des)
        {
            des = mp[st];
            printf(" %s-%s",st.c_str(),des.c_str());
        }
        printf("\n");
    }
}


相关文章推荐

Google中国2015校园招聘笔试Round D APAC Test Problem A. Cube IV

Problem Vincenzo decides to make cube IV but only has the budget to make a square maze. Its a perfe...

Google中国2015校园招聘笔试Round D APAC Test Problem B. GBus count

Problem There exists a straight line along which cities are built. Each city is given a number s...

Google中国2015校园招聘笔试Round D APAC Test Problem D. Itz Chess

Problem Given an arranged chess board with pieces, figure out the total number of different ways in...

Google中国2014校园招聘笔试Round A China New Grad Test Problem D. Cross the maze

Problem Edison, a robot, does not have a right hand or eyes. As a brave robot, he always puts his le...

Google中国2014校园招聘笔试Round B China New Grad Test Problem C. Hex

Problem This problem was inspired by a board game called Hex, designed independently by Piet Hein a...

Google中国2014校园招聘笔试Round A China New Grad Test Problem A. Read Phone Number

Problem Do you know how to read the phone numbers in English? Now let me tell you. For example, In C...

Google中国2014校园招聘笔试Round A China New Grad Test Problem B. Rational Number Tree

Problem Consider an infinite complete binary tree where the root node is 1/1 and left and right chil...

Google中国2014校园招聘笔试Round B China New Grad Test Problem B. Meet and party

Problem Little Sin lives in a Manhattan-grid city, a 2D plane where people can only go north, west, ...

Google中国2014校园招聘笔试Round B China New Grad Test Problem E. Ignore all my comments

Problem Good programmers write fabulous comments. Igor is a programmer and he likes the old C-style...

Google中国2014校园招聘笔试Round A China New Grad Test Problem E. Spaceship Defence

Problem The enemy has invaded your spaceship, and only superior tactics will allow you to defend it!...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Google中国2015校园招聘笔试Round D APAC Test Problem C. Sort a scrambled itinerary
举报原因:
原因补充:

(最多只允许输入30个字)