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  ,如需转载请自行联系原作者
相关文章
|
网络架构
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(单调栈)
|
1月前
|
C语言
【数据结构】栈和队列(c语言实现)(附源码)
本文介绍了栈和队列两种数据结构。栈是一种只能在一端进行插入和删除操作的线性表,遵循“先进后出”原则;队列则在一端插入、另一端删除,遵循“先进先出”原则。文章详细讲解了栈和队列的结构定义、方法声明及实现,并提供了完整的代码示例。栈和队列在实际应用中非常广泛,如二叉树的层序遍历和快速排序的非递归实现等。
185 9
|
1月前
|
存储 算法
非递归实现后序遍历时,如何避免栈溢出?
后序遍历的递归实现和非递归实现各有优缺点,在实际应用中需要根据具体的问题需求、二叉树的特点以及性能和空间的限制等因素来选择合适的实现方式。
32 1
|
23天前
|
存储 缓存 算法
在C语言中,数据结构是构建高效程序的基石。本文探讨了数组、链表、栈、队列、树和图等常见数据结构的特点、应用及实现方式
在C语言中,数据结构是构建高效程序的基石。本文探讨了数组、链表、栈、队列、树和图等常见数据结构的特点、应用及实现方式,强调了合理选择数据结构的重要性,并通过案例分析展示了其在实际项目中的应用,旨在帮助读者提升编程能力。
44 5
|
1月前
|
存储 算法 Java
数据结构的栈
栈作为一种简单而高效的数据结构,在计算机科学和软件开发中有着广泛的应用。通过合理地使用栈,可以有效地解决许多与数据存储和操作相关的问题。
|
1月前
|
存储 JavaScript 前端开发
执行上下文和执行栈
执行上下文是JavaScript运行代码时的环境,每个执行上下文都有自己的变量对象、作用域链和this值。执行栈用于管理函数调用,每当调用一个函数,就会在栈中添加一个新的执行上下文。