Maverick

never stop thinking

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

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


阅读更多
文章标签: C++ google 笔试
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭