poj 2021 Relative Relatives(典型数据结构题)

简介:
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3244   Accepted: 1405

Description

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

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: 
  1. Descendant Count - A line containing a single integer X (where 0 < X < 100) indicating the number of Ted's descendants.
  2. 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).

Output

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.

Sample Input

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

Sample Output

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

复制代码
#include <iostream>
#include <map>
#include <list>
#include <algorithm>
using namespace std;

//定义父子关系,键为父亲名字,值为儿子名字
multimap<string,string > relation;
//定义年龄图,键为名字,值为与父亲年龄的差值
map<string,int> age_map;

map<string,int> name_age;

//定义一个人,有名字和年龄
typedef struct
{
    string name;
    int age;
}Person;

void update(string name)
{
    int count;
    string cname;
    int age;
     //针对两个图分别做一个迭代器
    multimap<string,string >::iterator ire;
    map<string,int>::iterator iage;
    count = relation.count(name);
    if(count==0)return;
    ire = relation.find(name);
    age = name_age.find(name)->second;
    for(int c = 0; c < count; c++,ire++)
    {
        cname = ire->second;
        iage = age_map.find(cname);
        name_age.insert(make_pair(cname,age-(iage->second)));
        update(cname);
    }
}

int cmp(const void *a,const void *b)
{
    Person *x = (Person*)a;
    Person *y = (Person*)b;
    if(x->age<y->age)return 1;
    else if(x->age>y->age)return -1;
    else return x->name.compare(y->name);
}

int main()
{
    Person person[110];
    int n;//测试数目
    int X;//后代数目
    int to_age;//与父亲年龄的差值
    int count;
    int k,length;
    //父亲的名字,孩子的名字
    string fname,name;
    cin>>n;
    for(int i = 1; i <= n;i++)
    {
        k = 0;
        cin>>X;
        relation.clear();
        age_map.clear();
        name_age.clear();
        while(X--)
        {
            cin>>fname>>name>>to_age;
            relation.insert(make_pair(fname,name));//父子关系插入多键映射表
            age_map.insert(make_pair(name,to_age));//名字和年龄差插入age_map中
        }
        name_age.insert(make_pair("Ted",100));
        update("Ted");
        for(map<string,int>::iterator it = name_age.begin(); it != name_age.end(); ++it)
        {
            person[k].name = it->first;
            person[k].age = it->second;
            k++;
        }
        qsort(person,k,sizeof(person[0]),cmp);
        cout<<"DATASET "<<i<<endl;
        for(int m = 1; m < k; m++)
        cout<<person[m].name<<" "<<person[m].age<<endl;

    }
    return 0;
}
复制代码












本文转自NewPanderKing51CTO博客,原文链接: http://www.cnblogs.com/newpanderking/archive/2012/09/09/2677814.html  ,如需转载请自行联系原作者

相关文章
|
9月前
|
网络架构
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
POJ 3494 Largest Submatrix of All 1’s(单调栈)
POJ 3494 Largest Submatrix of All 1’s(单调栈)
|
4天前
|
机器学习/深度学习 算法 测试技术
【单调栈】3113. 边界元素是最大值的子数组数目
【单调栈】3113. 边界元素是最大值的子数组数目
|
1天前
栈的基本应用
栈的基本应用
10 3
|
1天前
栈与队列理解
栈与队列理解
8 1
|
2天前
|
存储 算法
数据结构与算法 栈与队列
数据结构与算法 栈与队列
7 0
数据结构与算法 栈与队列
|
2天前
|
C++
数据结构(共享栈
数据结构(共享栈
6 0