首页 > ACM题库 > HDU-杭电 > HDU 1311 Relative Relatives[解题报告] C++
2013
12-08

HDU 1311 Relative Relatives[解题报告] C++

Relative Relatives

问题描述 :

Today is Ted’s 100th birthday. A few weeks ago, you were selected by the family to contact all of Ted’s descendants and organize a surprise party. To make this task easier, you created an age-prioritized list of everyone descended from Ted. Descendants of the same age are listed in dictionary order.

The only materials you had to aid you were birth certificates. Oddly enough, these birth certificates were not dated. They simply listed the father’s name, the child’s name, and the father’s exact age when the baby was born.

输入:

Input to this problem will begin with line containing a single integer n indicating the number of data sets. Each data set will be formatted according to the following description.

A single data set has 2 components:

Descendant Count – A line containing a single integer X (where 0 < X < 100) indicating the number of Ted’s descendants.
Birth Certificate List – Data for X birth certificates, with one certificate’s data per line. Each certificate’s data will be of the format "FNAME CNAME FAGE" where:
FNAME is the father’s name.
CNAME is the child’s name.
FAGE is the integer age of the father on the date of CNAMEs birth.
Note:

Names are unique identifiers of individuals and contain no embedded white space.
All of Ted’s descendants share Ted’s birthday. Therefore, the age difference between any two is an integer number of years. (For those of you that are really picky, assume they were all born at the exact same hour, minute, second, etc… of their birth year.)
You have a birth certificate for all of Ted’s descendants (a complete collection).

输出:

For each data set, there will be X+1 lines of output. The first will read, "DATASET Y", where Y is 1 for the first data set, 2 for the second, etc. The subsequent X lines constitute your age-prioritized list of Ted’s descendants along with their ages using the format "NAME AGE". Descendants of the same age will be listed in dictionary order.

样例输入:

2
1
Ted Bill 25
4
Ray James 40
James Beelzebub 17
Ray Mark 75
Ted Ray 20

样例输出:

DATASET 1
Bill 75
DATASET 2
Ray 80
James 40
Beelzebub 23
Mark 5

谈不上什么算法,直接贴代码:

 

#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

struct people
{
	int age;
	string name;
}p[100];

bool cmp(struct people a,struct people b)
{
	if(a.age!=b.age)
		return a.age>b.age;
	else
		return a.name<b.name;
}

int n;
string f[100],c[100];
int year[100];

int main()
{
	int t,count=1;
	cin>>t;
	int k;

	while(t--)
	{
		cin>>n;

		p[0].name="Ted";
		p[0].age=100;
		k=1;

		for(int i=0;i<n;i++)
			cin>>f[i]>>c[i]>>year[i];

		for(i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(f[j]==p[i].name)
				{
					p[k].name=c[j];
					p[k].age=p[i].age-year[j];
					k++;
				}
			}
		}
		sort(p,p+n+1,cmp);

		cout<<"DATASET "<<count++<<endl;
		for(i=1;i<=n;i++)
		{
			cout<<p[i].name<<" "<<p[i].age<<endl;
		}
	}
	return 0;
}

 

解题报告转自:http://blog.csdn.net/arsenal1109389480/article/details/7658899


  1. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。

  2. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c

  3. “再把所有不和该节点相邻的节点着相同的颜色”,程序中没有进行不和该节点相邻的其他节点是否相邻进行判断。再说求出来的也不一样是颜色数最少的

  4. [email protected]