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=; i<=t; i++)
{
map<string,string>next,pre;//定义两个保存前序,后序的map
int n;
cin >> n;
n=n-;
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 ;
}

POJ 2491 Scavenger Hunt map的更多相关文章

  1. POJ 1066 Treasure Hunt(线段相交判断)

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4797   Accepted: 1998 Des ...

  2. poj 2418 Hardwood Species (map)

    题目:http://poj.org/problem?id=2418 在poj 上交题总是有各种错误,再次感叹各个编译器. c++ AC代码,G++为超时,上代码: #include<cstdio ...

  3. poj 2503 Babelfish (查找 map)

    题目:http://poj.org/problem?id=2503 不知道为什么 poj  的 数据好像不是100000,跟周赛的不一样 2000MS的代码: #include <iostrea ...

  4. POJ 1066 Treasure Hunt (线段相交)

    题意:给你一个100*100的正方形,再给你n条线(墙),保证线段一定在正方形内且端点在正方形边界(外墙),最后给你一个正方形内的点(保证不再墙上) 告诉你墙之间(包括外墙)围成了一些小房间,在小房间 ...

  5. POJ 1840 Eqs 二分+map/hash

    Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...

  6. 简单几何(线段相交) POJ 1066 Treasure Hunt

    题目传送门 题意:从四面任意点出发,有若干障碍门,问最少要轰掉几扇门才能到达终点 分析:枚举入口点,也就是线段的两个端点,然后选取与其他线段相交点数最少的 + 1就是答案.特判一下n == 0的时候 ...

  7. POJ 1066 Treasure Hunt(计算几何)

    题意:给出一个100*100的正方形区域,通过若干连接区域边界的线段将正方形区域分割为多个不规则多边形小区域,然后给出宝藏位置,要求从区域外部开辟到宝藏所在位置的一条路径,使得开辟路径所需要打通的墙壁 ...

  8. poj 1066 Treasure Hunt

    http://poj.org/problem?id=1066 #include <cstdio> #include <cstring> #include <cmath&g ...

  9. POJ 1066 Treasure Hunt(相交线段&amp;&amp;更改)

    Treasure Hunt 大意:在一个矩形区域内.有n条线段,线段的端点是在矩形边上的,有一个特殊点,问从这个点到矩形边的最少经过的线段条数最少的书目,穿越仅仅能在中点穿越. 思路:须要巧妙的转换一 ...

随机推荐

  1. Linux 常用命令及使用方法

    1.  type   :查询命令 是否属于shell解释器 2.  help  : 帮助命令3.  man : 为所有用户提供在线帮助4.  ls  : 列表显示目录内的文件及目录 -l    以长格 ...

  2. js获得页面get跳转的参数

    通过js获得页面跳转参数 页面通过window.location.href或通过window.parent.location.href进行页面跳转,在新的页面如何获得相应的参数呢? window.lo ...

  3. Pyenv虚拟环境的创建(虚拟机)

    创建pyenv虚拟环境 sudo yum install openssl* 安装其所需要的库文件 git clone https://github.com/yyuu/pyenv.git ~/.pyen ...

  4. 使用Arthas 获取Spring ApplicationContext还原问题现场

    ## 背景 最近来了个实习僧小弟,安排他实现对目标网站 连通性检测的小功能,简单讲就是将下边的shell 脚本换成Java 代码来实现 ``` 1#!/bin/bash 2URL="http ...

  5. cogs 1254. 最难的任务 Dijkstra + 重边处理

    1254. 最难的任务 ★   输入文件:hardest.in   输出文件:hardest.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述] 这个真的很难.算出 123 ...

  6. 一文速览Vue全栈

    一文速览Vue全栈 原创: 新哥Lewis 天道酬勤Lewis 7月7日 Vue 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用,专注于声明式渲染视图 ...

  7. Android——倒计时跳转+sharedpreferences

    public class MainActivity extends Activity { // 3秒钟后,从图1跳转到图2(10) private Handler handler=new Handle ...

  8. Codeforces 343D Water Tree

    题意简述 维护一棵树,支持以下操作: 0 v:将以v为跟的子树赋值为1 1 v:将v到根节点的路径赋值为0 2 v:询问v的值 题解思路 树剖+珂朵莉树 代码 #include <set> ...

  9. 监听input实时输入

    // 用Jquery实现: $('#input-element').on('input',()=>{ console.log("你按了一下111");}) // 用Jquer ...

  10. Spark基本函数学习

    package cn.itcast.spark.czh import org.apache.spark.{SparkConf, SparkContext} object TestFun { def m ...