ZOJ 1076 Gene Assembly

本文介绍了一种使用贪心算法和合并排序方法解决DNA序列中寻找最长基因链的问题。通过将基因按起始位置排序,并利用静态链表记录最长链的节点,最终输出最长基因序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

////////////////////////////////////////////////////////
//Gene Assembly
//DNA序列中最多gene序列
//用贪心法,先以基因头位置对基因排序,再用静态链存储、搜索

 

#include<iostream>
using namespace std;
#define MAX 1005
long st[MAX][2],output[MAX];  //st作为静态链表,output为输出gene的顺序而设


class Point
{
public:
    long x;
    long y;
    long No;
    bool operator<=(Point a)  //重载<=为了能在合并排序中使用
    {
        return (x<=a.x);
    }
};


//以下是合并排序
///////////////////////////////////////////////////////////////////


template <class Type>              
void Merge(Type c[],Type d[],long l, long m, long r)
{
    long p,k=l,i=l,j=m+1;
    while(i<=m && j<=r)
        if(c[i]<=c[j])  d[k++]=c[i++];
        else d[k++]=c[j++];

    if(j>r)
        for(p=i;p<=m;p++)  d[k++]=c[p];
    else
        for(p=j;p<=r;p++)  d[k++]=c[p];
}

 

template <class Type>
void MergePass(Type x[],Type y[], long s, long n)
{
    long i=0,j;
    while(i<=n-2*s)
    {
        Merge(x, y, i, i+s-1,i+2*s-1);
        i+=2*s;
    }
    if(i+s<n)  Merge(x, y, i, i+s-1, n-1);
    else for(j=i;j<n;j++) y[j]=x[j];
}

 

template <class Type>
void MergeSort(Type a[],long n)
{
    Type *b=new Type [n];
    long s=1;
    while(s<n)
    {
        MergePass(a,b,s,n);
        s+=s;
        MergePass(b,a,s,n);
        s+=s;
    }
}

 

///////////////////////////////////////////////////////////////////

 

int main()
{
    long i,j,k,n,state,max;
    Point node[MAX];
    while(cin>>n && n!=0)
    {        
        node[0].x=0;
        node[0].y=0;
        node[0].No=0;
        k=1;
        for(i=1;i<=n;i++)
        {
            cin>>node[i].x>>node[i].y;
            node[i].No=k++;                     //输入时就先把gene的序号用No记录
        }
        MergeSort(node,n+1);
    
        //贪心法生成以静态链存储的树
        st[0][0]=-1;         //st[i][0]记录前一节点
        st[0][1]=0;          //st[i][1]记录层数
        for(i=1;i<=n;i++)
        {
            max=-1;
            for(j=0;j<i;j++)
            {
                if(node[i].x>node[j].y && max<st[j][1])
                {
                    state=j;
                    max=st[j][1];
                }
            }
            st[i][0]=state;
            st[i][1]=max+1;
        }

        //找出最大层数的节点位置
        max=0;
        for(i=1;i<=n;i++)
        {
            if(st[i][1]>max)
            {
                max=st[i][1];
                state=i;
            }
        }
        //把最大链记录到output中
        j=0;
        while(state!=0)
        {
            output[j++]=node[state].No;
            state=st[state][0];
        }

        for(i=j-1;i>=0;i--)
        {
            cout<<output[i];
            if(i!=0)
                cout<<" ";
        }
        cout<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/VRS_technology/archive/2010/05/11/1732299.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值