路径图排序 【绿色】
时间限制(普通/Java) : 1000 MS/ 3000 MS 运行内存限制 : 65536 KByte
总提交 : 179 测试通过 : 19
总提交 : 179 测试通过 : 19
比赛描述
南邮野生物基金会(NUPT-WF)的志愿者近期去南京老山国家森林公园,寻找住在森林附近濒临灭绝的动物。NUPT-WF志愿者得到一份老山国家森林公园地图,该森林公园包括许多空地,和连接空地的路径。不同种濒危动物住在每条路径上,没有动物住在空地上。
NUPT-WF希望从森林公园找到一些濒危动物。但是它们的路径图太过混乱,因为空地和动物未按特定序列给出。现请你帮助NUPT-WF志愿者给南京老山国家森林公园的路径图排序。
输入
动物以单词表示,空地用数字表示,最多有500个空地。
空地0总是森林公园的入口。输入为未分类的森林公园的路径图。
每一行描述两个空地之间的路径,表示为一对数字和住在路径上的一列动物。
输出
输出为一个排序后的路径图,空地数字从0开始增长,动物名从a到z排序。路径由路径任一端的空地号进行排序。所有住在路径上的动物依字母顺序排序。
样例输入
1 0 puma lynx
2 0 puma
1 2 vole
样例输出
0 1 lynx puma
0 2 puma
1 2 vole
题目来源
“IBM南邮杯”团队赛2009
//路径图排序 【绿色】
//string输出有时候不能printf,只能cout,或者加上c_
//第一种是string输入,注意结束crtl+z —— AC
//第二种是gets的方法的,但是Presentation Error at Test 5,原因未知。。。
#include<iostream>
#include<set>
#include<string>
using namespace std;
#define SIZE 500
struct Map
{
set<string> s;
}map[SIZE][SIZE];
int main()
{
int x = -100, y = -100, flag = 0;
char str[10];
while(scanf("%s",str) != EOF)
{
if(str[0] >= 'a' && str[0] <= 'z')
{
map[x][y].s.insert(str);
flag = 1;
}
else
{
int tmp = 0;
if(flag == 1) {x = -100; y = -100; flag = 0;} // init
int i = 0;
while(str[i] != '\0')
{
tmp = tmp * 10 + str[i] - '0';
i++;
}
if(x == -100)
{
x = tmp;
continue; // back to scanf str
}
else y = tmp;
if(x > y) {int tmp=x; x=y; y=tmp;}
}
}
set<string>::iterator it;
for(int i=0;i<SIZE;i++)
{
for(int j=i+1;j<SIZE;j++)
{
if(!map[i][j].s.empty())
{
printf("%d %d",i,j);
for(it=map[i][j].s.begin();it!=map[i][j].s.end();it++)
cout<<" "<<*it;
printf("\n");
}
}
}
return 0;
}
//附:第二种方法gets输入的,Presentation Error at Test 5
/*
#include<iostream>
#include<string>
#include<set>
using namespace std;
struct Map
{
set<string> s;
}map[500][500];
int main()
{
int x, y;
char str[1000];
while(scanf("%d %d",&x,&y) != EOF)
{
gets(str);
if(x > y) {int tmp=x; x=y; y=tmp;}
//printf("%d %d",x,y);
//printf("%s\n",str);
int i = 1;
char tmp[1000] = {0};
int num = 0;
while(str[i] != '\0')
{
if(str[i] == ' ')
{
tmp[num++] = '\0';
//printf("insert what:%s\n",tmp);
map[x][y].s.insert(tmp);
num = 0;
i++;
}
else
tmp[num++] = str[i++];
}
tmp[num++] = '\0';
//printf("insert what:%s\n",tmp);
map[x][y].s.insert(tmp);
}
//
set<string>::iterator it;
for(int i=0;i<500;i++)
{
for(int j=i+1;j<500;j++)
{
if(!map[i][j].s.empty())
{
printf("%d %d",i,j);
for(it=map[i][j].s.begin();it!=map[i][j].s.end();it++)
cout<<" "<<*it;
printf("\n");
}
}
}
return 0;
}
*/