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

13 篇文章 0 订阅
12 篇文章 0 订阅

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");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值