POJ 2491 Scavenger Hunt map

Scavenger Hunt
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2848 Accepted: 1553

Description

Background 
Bill has been the greatest boy scout in America and has become quite a superstar because he always organized the most wonderful scavenger hunts (you know, where the kids have to find a certain route following certain hints). Bill has retired now, but a nationwide election quickly found a successor for him, a guy called George. He does a poor job, though, and wants to learn from Bill's routes. Unfortunately Bill has left only a few notes for his successor. 
Problem 
Bill never wrote down his routes completely, he only left lots of little sheets on which he had written two consecutive steps of the routes. He then mixed these sheets and memorized his routes similarly to how some people learn for exams: practicing again and again, always reading the first step and trying to remember the following. This made much sense, since one step always required something from the previous step. 
George however would like to have a route written down as one long sequence of all the steps in the correct order. Please help him make the nation happy again by reconstructing the routes.

Input

The first line contains the number of scenarios. Each scenario describes one route and its first line tells you how many steps (3 <= S <= 333) the route has. The next S-1 lines each contain one consecutive pair of the steps on the route separated by a single space. The name of each step is always a single string of letters.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print S lines containing the steps of the route in correct order. Terminate the output for the scenario with a blank line.

Sample Input

2
4
SwimmingPool OldTree
BirdsNest Garage
Garage SwimmingPool
3
Toilet Hospital
VideoGame Toilet

Sample Output

Scenario #1:
BirdsNest
Garage
SwimmingPool
OldTree

Scenario #2:
VideoGame
Toilet
Hospital

Source

TUD Programming Contest 2005, Darmstadt, Germany
这个题目的大概意思是找顺序,也可以说是找线索,先找第一个。
SwimmingPool OldTree(SwimmingPool在OldTree前面)
BirdsNest Garage 
Garage SwimmingPool
由此可知 BirdsNest -> Garage -> SwimmingPool -> OldTree.
然后再按这顺序每行一个名词输出.
这个题目运用的方法是用两个map保存前序和后序
然后找到第一个,再按顺序输出
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <string>
using namespace std;
int main()
{
    int t;
    cin >> t;
    for(int i=1; i<=t; i++)
    {
        map<string,string>next,pre;//定义两个保存前序,后序的map
        int n;
        cin >> n;
        n=n-1;
        string tmp,s,s1;
        while(n--)
        {
            cin >> s >> s1;
            next[s] = s1;
            pre[s1] = s;
        }
        while(pre[s]!="")
            s= pre[s];//当前序为空的时候,说明找到第一个
        printf("Scenario #%d:\n",i);
        cout << s << endl;//先输出第一个
        while(next[s]!="")//当后面不为空的时候输出下一个,最后一个不输出
        {
            cout << next[s] << endl;
            s = next[s];//让s到下一个
        }
        cout << endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/l609929321/p/6547080.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值