zoukankan      html  css  js  c++  java
  • toj 1705 Scavenger Hunt

    1705.   Scavenger Hunt
    Time Limit: 1.0 Seconds   Memory Limit: 65536K
    Total Runs: 399   Accepted Runs: 214    Multiple test files



    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
    Submit   List    Runs   Forum   Statistics

    #include <iostream>
    #include 
    <string>
    #include 
    <map>
    #define MAX 700
    using namespace std;
    int path[MAX];
    map
    <string,int>M;
    map
    <string,int>::iterator p;
    string str[MAX];
    int t,n;
    int main()
    {
        cin
    >>t;
        
    int zz=1;
        
    while(t--)
        {
            cin
    >>n;
            
    string a,b;
            M.clear();
            
    int k,i,j;
            
    int numa,numb;
            
    for(i=1;i<=n;i++)
            {
                path[i]
    =-1;
            }
            cin
    >>a>>b;
            M[a]
    =1;
            M[b]
    =2;
            str[
    1]=a;
            str[
    2]=b;
            path[
    2]=1;
            k
    =3;
            
    for(i=2;i<n;i++)
            {
                cin
    >>a>>b;
                p
    =M.find(a);
                
    if(p==M.end())
                {
                    M[a]
    =k;
                    str[k]
    =a;
                    numa
    =k;
                    k
    ++;
                }
                
    else
                {
                    numa
    =p->second;
                }
                p
    =M.find(b);
                
    if(p==M.end())
                {
                    M[b]
    =k;
                    str[k]
    =b;
                    numb
    =k;
                    k
    ++;
                }
                
    else
                {
                    numb
    =p->second;
                }
                path[numb]
    =numa;
            }
            
    int start;
            printf(
    "Scenario #%d:\n",zz++);
            
    for(i=1;i<=n;i++)
            {
                
    if(path[i]==-1)
                {
                    start
    =i;
                    cout
    <<str[i]<<endl;
                    
    break;
                }
            }
            
    int kk=0;
            
    int next=start;
            
    while(1)
            {
                
    if(kk>=n)
                    
    break;
                
    for(i=1;i<=n;i++)
                {
                    
    if(path[i]==next)
                    {
                        cout
    <<str[i]<<endl;
                        
    break;
                    }
                }
                next
    =i;
                kk
    ++;
            }
            cout
    <<endl;
        }
        
    return 0;
    }
  • 相关阅读:
    在一个字符串中找到第一个只出现一次的字符
    查找最小的k个数
    动规:最大上升子序列
    平衡二叉树
    【笔记】php和mysql结合 搞了一个表出来
    设计模式心得(既设计模式篇终章):描述设计模式时的通用公式
    分享系列 之 linux IO原理与几种零拷贝机制的实现
    近期分享:BIO 与 NIO 的实质区别到底是什么?
    源码阅读笔记 之 ThreadLocal —— 不复杂,却有点绕的一个 per thread API
    小脑袋瓜充满了问号:为什么AMQP可以叫做 Advanced?JMS就要low一等吗?
  • 原文地址:https://www.cnblogs.com/forever4444/p/1458415.html
Copyright © 2011-2022 走看看