ZOJ 1076 Gene Assembly(贪心算法)

Gene Assembly
Time Limit: 2 Seconds      
Memory Limit: 65536 KB
Statement of the Problem

With the large amount of genomic DNA sequence data being made available, it is becoming more important to find genes (parts of the genomic DNA which are responsible for the synthesis of proteins) in these sequences. It is known that for eukaryotes (in contrast to prokaryotes) the process is more complicated, because of the presence of junk DNA that interrupts the coding region of genes in the genomic sequence. That is, a gene is composed by several pieces (called exons) of coding regions. It is known that the order of the exons is maintained in the protein synthesis process, but the number of exons and their lengths can be arbitrary.

Most gene finding algorithms have two steps: in the first they search for possible exons; in the second they try to assemble a largest possible gene, by finding a chain with the largest possible number of exons. This chain must obey the order in which the exons appear in the genomic sequence. We say that exon i appears before exon j if the end of i precedes the beginning of j.

The objective of this problem is, given a set of possible exons, to find the chain with the largest possible number of exons that cound be assembled to generate a gene.

Input Format

Several input instances are given. Each instance begins with the number 0 < n < 1000 of possible exons in the sequence. Then, each of the next n lines contains a pair of integer numbers that represent the position in which the exon starts and ends in the genomic sequence. You can suppose that the genomic sequence has at most 50000 basis. The input ends with a line with a single 0.

Output Format

For each input instance your program should print in one line the chain with the largest possible number of exons, by enumerating the exons in the chain. If there is more than one chain with the same number of exons, your program can print anyone of them.

Sample Input

6
340 500
220 470
100 300
880 943
525 556
612 776
3
705 773
124 337
453 665
0

Sample Output

3 1 5 6 4
2 3 1

题意(这道题主要是说寻找基因链中尽可能多的外显子基因链(基因识别的两个步骤:第一:寻找可能的外显子,第二:通过寻找外显子寻找一条拥有尽可能多的外显子的基因链,尽可能大地拼接一个基因。)这条链必须遵循外显子出现在基因序列中的顺序,外显子i在外显子j的前面的条件是i的末尾必须在j开头的前面。)

本题目标是:给定一组可能的外显子,找出一条拥有尽可能多的外显子的外显子链,拼接成一个基因。

(输出:链中的外显子,并占一行,假如有多条链,但外显子数相同,那么可以输出其中的一条)

————————————————————————————————————————

理解:这道题和(南阳理工上的”会场安排“)那道题是一样的思路(安排尽量多的会场)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>

using namespace std;

struct DNA
{
    int si;///外显子的起始位置
    int ei;///外显子的结束位置
    int num;  ///外显子的序号
}s[1005];
int cmp(DNA a,DNA b)
{
    if(a.ei!=b.ei)
        return a.ei<b.ei;///按照外显子的结束位置从小到大排列
    return a.si<b.si;

}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
      for(int i=0;i<n;i++)
      {
          s[i].num=i+1;      ///外显子的输入序号
          scanf("%d %d",&s[i].si,&s[i].ei);
      }
       qsort(s,s+n,cmp);<pre name="code" class="cpp">#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>

using namespace std;

struct DNA
{
    int si;///外显子的起始位置
    int ei;///外显子的结束位置
    int num;  ///外显子的序号
}s[1005];

int cmp(const void *a,const void  *b)
{
    return ((struct DNA *)a)->ei-((struct DNA *)b)->ei;

}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
      for(int i=0;i<n;i++)
      {
          s[i].num=i+1;      ///外显子的输入序号
          scanf("%d %d",&s[i].si,&s[i].ei);
      }
       qsort(s,n,sizeof(DNA),cmp);///快排
      int x=s[0].ei;///第一个一定符合
           printf("%d",s[0].num);
      for(int i=0;i<n;i++)
      {
          if(s[i].si>x)
          {
              x=s[i].ei;
              printf(" %d",s[i].num);
          }
          else
            continue;///若不符合,则表示这个链中的外显子不存在
      }
      printf("\n");

    }
    return 0;
}

int x=s[0].ei;///第一个一定符合 printf(“%d”,s[0].num); for(int i=0;i<n;i++) { if(s[i].si>x) { x=s[i].ei; printf(” %d”,s[i].num); } else continue;///若不符合,则表示这个链中的外显子不存在 } printf(“\n”); } return 0;}

    原文作者:贪心算法
    原文地址: https://blog.csdn.net/jingttkx/article/details/51211244
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞