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...
  • maverick1990
  • maverick1990
  • 2014年11月13日 23:28
  • 1151

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...
  • maverick1990
  • maverick1990
  • 2014年11月14日 14:50
  • 994

Google APAC 2016 University Graduates Test Round D

之前的Round A晋级了...但是HR说今年7月因为Codejam的原因面过一次了不给再面了..所以Round C和Round B都没怎么做..这次最后一场Round D还是水了一发...虽然太弱了...
  • kk303
  • kk303
  • 2015年11月10日 09:33
  • 704

Catalan数和应用 & 2015 google APAC round 2 problem D 括号配对问题

Catalan数是排列组合中很重要的一种数
  • xiaonimao
  • xiaonimao
  • 2014年09月30日 20:31
  • 571

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 ...
  • maverick1990
  • maverick1990
  • 2014年11月14日 11:44
  • 1186

Google APAC 2017 University Test Round E

Google APAC 2017 University Test Round E
  • u012734359
  • u012734359
  • 2016年11月06日 16:24
  • 730

Google 2015 APAC Round A Problem C - Addition

这个题算是一个比较经典的graph theory的问题,乍看之下好像要解线性方程组实际上不用。由于所有的方程都是两个两个连着的而且每个方程只有两个变量相加,因此我们可以确定: 给定a,b, 如果能够找...
  • sinat_17277843
  • sinat_17277843
  • 2015年09月01日 16:05
  • 68

CODE JAM APAC 2015 round B: Problem A. Password Attacker(小球放进盒子的计数问题)

参加了这一轮APAC,结果只做对这第一题,而且解法还是
  • xiaonimao
  • xiaonimao
  • 2014年09月17日 13:18
  • 1033

Round A APAC Test 2017 java完整代码及解答

第一题:country leader package googleJam; import java.io.FileInputStream; import java.io.FileOutputStre...
  • u112222222
  • u112222222
  • 2016年08月14日 21:45
  • 654

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...
  • maverick1990
  • maverick1990
  • 2014年11月13日 23:37
  • 787
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Google中国2015校园招聘笔试Round D APAC Test Problem C. Sort a scrambled itinerary
举报原因:
原因补充:

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