UVA939 Genes【map】

For many years, the Genetic Evolution Laboratory (GEL) has been studying the evolution of various genetic diseases, by collecting, for each disease, data from a large population, over several generations. For each person, it is known whether the gene is dominant, recessive, or non-existent. Based on this data, scientists formulate an hypothesis on how the disease is transmitted from parents to children. However, it is tedious to check by hand if the data matches the hypothesis, so GEL asked you to automate this task for them.
    The idea is simple: the program will, given the parent-child relationships and a fixed hypothesis, calculate for all people whether they have the gene or not, and in the former case whether it is dominant or not. This result is written to a text file, which will later be compared (using a standard file differencing tool) with the data that GEL collected. If there are no differences, the hypothesis is valid. Any mismatch will give clues to GEL’s scientists on how they should improve the hypothesis.
    Given a set of parent-child relationships, and the status of the gene for all those people whose parents are not in the data set, compute the status of the gene for all people in the data set, according to the following hypothesis:
• the child has the gene if, and only if, both parents have it or it is dominant in one of the parents; and
• the child’s gene is dominant if, and only if, the gene is dominant in both parents or dominant in one and recessive in the other parent.
Input
The first line contains an integer N (1 ≤ N ≤ 3100), which is the number of lines of the data set.
    Each of the following N lines contains a pair of non-empty strings, separated by a space. All strings are at most 20 characters long, and no string contains a blank (tab or space).
    The first string is the name of a person. The second string is either

  1. ‘non-existent’, ‘recessive’, or ‘dominant’; or
  2. the name of another person (the child).

    The first case is to indicate the status of the gene for a person whose parents are not part of the data set. The second case is to indicate that the first person is a parent of the second one.
    All people have distinct names and none is “non-existent”, “recessive”, or “dominant”.
    For each person, either both or none of the parents are part of the data set.
Output
The output consists of one line per person occurring in the data set.
    Each line has two strings, separated by a space. The first string is the name of the person, and the second string indicates the status of the gene, in the same way as in the input.
    The output must be ordered alphabetically by name.
Sample Input
7
John dominant
Mary recessive
John Susan
Mary Susan
Peter non-existent
Susan Marta
Peter Marta
Sample Output
John dominant
Marta recessive
Mary recessive
Peter non-existent
Susan dominant

问题链接UVA939 Genes
问题简述:(略)
问题分析
    先给题解代码,暂时不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA939 Genes */

#include <bits/stdc++.h>

using namespace std;

const int N = 3100;
int genes[N];

map<string, int> mps2i = {{"non-existent", -1}, {"recessive", 0}, {"dominant", 1}};
map<int, string> mpi2s ={{-1, "non-existent"}, {0, "recessive"}, {1, "dominant"}};

map<string, int> m;
vector<int> par[N];

int seq = 0;
int get(string& s)
{
    if(m.find(s) != m.end())
        return m[s];
    else
        return m[s] = ++seq;
}

int cal(int a, int b)
{
    if(a + b == 0)
        return 0;
    else if(a + b > 0)
        return 1;
    else
        return -1;
}

int dp(int k)
{
    if(genes[k] < 2)
        return genes[k];
    else
        return genes[k] = cal(dp(par[k][0]), dp(par[k][1]));
}

int main()
{
    int n;
    string a, b;

    memset(genes, 127, sizeof(genes));

    cin >> n;
    while(n--) {
        cin >> a >> b;
        if(mps2i.find(b) != mps2i.end())
            genes[get(a)] = mps2i[b];
        else
            par[get(b)].push_back(get(a));
    }

    for(auto &a : m)
        cout << a.first << " " << mpi2s[dp(a.second)] << endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值