hdu 2597 Sorted Trail Map

Sorted Trail Map

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 208    Accepted Submission(s): 39


Problem Description
Volunteers from the World Wildlife Foundation (WWF) have arrived in Springfield in a WWF truck.
They are collecting endangered animals living in the nearby Gump Forest, and are asking Lisa for help.
The WWF volunteers have given Lisa a map of the Gump Forest. The forest contains a number of clearings, and trails connecting the clearings. Different types of endangered animals live on each trail, no animals live in the clearings.
The WWF are hoping to collect some endangered animals from Gump Forest. But their trail map is too confusing, since clearings and animals are listed in no particular order. Lisa decides to help the WWF volunteers by making up a sorted trail map of Gump Forest.
 

Input
Animals are given as words(lowercases). Clearings are given as numbers. There may be up to 500 clearings.
Clearing 0 is always the entrance to Gump Forest. The input is the unsorted Gump Forest trail map.
Each line describes a trail between two clearings as a pair of numbers, followed by a list of animals living on the trail.
Every test case ends with "-1 -1".
 

Output
The program output is a sorted trail map, where clearing numbers go up from 0, and animal names go from a to z. Trails are sorted by the number of the clearings at either end of the trail. All animals living on the trail are sorted in alphabetical order.
 

Sample Input
  
  
0 1 puma 2 3 toad 1 2 frog -1 -1 1 0 puma lynx 2 0 toad 1 2 vole -1 -1
 

Sample Output
  
  
0 1 puma 1 2 frog 2 3 toad 0 1 lynx puma 0 2 toad 1 2 vole
 

Source
 

Recommend
lcy

题意:
输入  x y  之后输入未知个单词        表示一条路上(双向)有这么多个濒危动物 ,单词是这些动物的名字
输入 -1 -1 的时候  对刚才的输入进行输出
输出的时候   路径首先按照开端从小到大的顺序排列  之后按末端从小到大      而路上的动物则按照字典序进行排列
注意 对于x y,  x y都可以做开端的


Trails are sorted by the number of the clearings at either end of the trail.  主要是这句话 一开始理解成了按照空地的出现个数进行排序 结果WA了好几遍
后来请教了以为大神才知道是空地的编号的意思

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<set>
#include<string>
#include<iostream>
using namespace std;
#define size 600
char s[size];
struct haha
{
    set<string>sset;
}mp[size][size];
void out();
int main()
{
    int x=-100,y=-100,i,j,flag=0;
    while(scanf("%s",s)!=EOF)
    {
          if(s[0]>='a'&&s[0]<='z')
          {
               mp[x][y].sset.insert(s);
               flag=1;
          }
          else
          {
              int n=0;
              if(flag==1) {x=-100;y=-100;flag=0;}
              if(s[0]=='-')
              {
                  if(x==-100) {x=-1; continue;}
                  else
                  {
                      if(x==-1)
                      {
                          out();
                          x=-100;
                          y=-100;
                          for(i=0;i<size;i++)
                              for(j=0;j<size;j++)
                                  mp[i][j].sset.clear();
                          continue;
                      }
                  }
              }
              i=0;
              while(s[i]!='\0')
              {
                  n=n*10+s[i]-'0';
                  i++;
              }
              if(x==-100) {x=n;continue;}
              else y=n;
              if(x>y)  {int temp=x;x=y;y=temp;}
          }
    }
    return 0;
}
void out()
{
    int i,j;
    set<string>::iterator it;
       for(i=0;i<size;i++)
          for(j=i;j<size;j++)
          {
              if(!mp[i][j].sset.empty())
              {
                  printf("%d %d",i,j);
                  for(it=mp[i][j].sset.begin();it!=mp[i][j].sset.end();it++)
                      cout<<" "<<*it;
                printf("\n");

              }
          }
}

其实没有必要这样输入 可以先输入 x y   如果不是-1 -1  则继续gets   一下把所有单词输入   上面的做法有点麻烦了

    
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值