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(单调栈)
|
10月前
|
C语言
【数据结构】栈和队列(c语言实现)(附源码)
本文介绍了栈和队列两种数据结构。栈是一种只能在一端进行插入和删除操作的线性表,遵循“先进后出”原则;队列则在一端插入、另一端删除,遵循“先进先出”原则。文章详细讲解了栈和队列的结构定义、方法声明及实现,并提供了完整的代码示例。栈和队列在实际应用中非常广泛,如二叉树的层序遍历和快速排序的非递归实现等。
856 9
|
10月前
|
存储 算法
非递归实现后序遍历时,如何避免栈溢出?
后序遍历的递归实现和非递归实现各有优缺点,在实际应用中需要根据具体的问题需求、二叉树的特点以及性能和空间的限制等因素来选择合适的实现方式。
215 59
|
3月前
|
编译器 C语言 C++
栈区的非法访问导致的死循环(x64)
这段内容主要分析了一段C语言代码在VS2022中形成死循环的原因,涉及栈区内存布局和数组越界问题。代码中`arr[15]`越界访问,修改了变量`i`的值,导致`for`循环条件始终为真,形成死循环。原因是VS2022栈区从低地址到高地址分配内存,`arr`数组与`i`相邻,`arr[15]`恰好覆盖`i`的地址。而在VS2019中,栈区先分配高地址再分配低地址,因此相同代码表现不同。这说明编译器对栈区内存分配顺序的实现差异会导致程序行为不一致,需避免数组越界以确保代码健壮性。
44 0
栈区的非法访问导致的死循环(x64)
232.用栈实现队列,225. 用队列实现栈
在232题中,通过两个栈(`stIn`和`stOut`)模拟队列的先入先出(FIFO)行为。`push`操作将元素压入`stIn`,`pop`和`peek`操作则通过将`stIn`的元素转移到`stOut`来实现队列的顺序访问。 225题则是利用单个队列(`que`)模拟栈的后入先出(LIFO)特性。通过多次调整队列头部元素的位置,确保弹出顺序符合栈的要求。`top`操作直接返回队列尾部元素,`empty`判断队列是否为空。 两题均仅使用基础数据结构操作,展示了栈与队列之间的转换逻辑。
|
8月前
|
存储 C语言 C++
【C++数据结构——栈与队列】顺序栈的基本运算(头歌实践教学平台习题)【合集】
本关任务:编写一个程序实现顺序栈的基本运算。开始你的任务吧,祝你成功!​ 相关知识 初始化栈 销毁栈 判断栈是否为空 进栈 出栈 取栈顶元素 1.初始化栈 概念:初始化栈是为栈的使用做准备,包括分配内存空间(如果是动态分配)和设置栈的初始状态。栈有顺序栈和链式栈两种常见形式。对于顺序栈,通常需要定义一个数组来存储栈元素,并设置一个变量来记录栈顶位置;对于链式栈,需要定义节点结构,包含数据域和指针域,同时初始化栈顶指针。 示例(顺序栈): 以下是一个简单的顺序栈初始化示例,假设用C语言实现,栈中存储
331 77