poj1007 DNA Sorting

//题意:给一个n, m分别代表列和行数,然后要求计算每一行的逆序数,按照逆序数从小到大的顺序排列,什么是逆序数:ATCGGCA 这个序列若正常顺序是AACCGGT,此时它的下标为1的T,下标为3,4的G,下标为5的C分别没有按照要求放置,则逆序数为:T:5;G(下标为3):2; G(下标为4):2;C:1,5+2+2+1 = 10,则逆序数为10。【注意】若相同的两个逆序数序列按原来的位置放置。

//思路:计算每一行的逆序数,用sort排序,这里处理一下相同的时候,然后输出即可,具体见代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

struct node{
    int data;
    int index;
}num[120];

bool cmp(const node a, const node b)
{
    if(a.data == b.data){    //这里就是当相同的逆序数时,按照下标小的顺序输出
        return a.index < b.index;
    }
    return a.data < b.data;
}

void cal(char **c, int a, int len)
{
    int A = 0, C = 0, G = 0, T = 0, cnt = 0;
    for(int i = len-1; i >= 0; i--){
        switch(c[a][i]){
        case 'A':
            A++;
            break;
        case 'C':
            cnt = cnt + A;
            C++;
            break;
        case 'G':
            cnt = cnt + A + C ;
            G++;
            break;
        case 'T':
            cnt = cnt + A + C + G;
            break;
        }
    }
    num[a].data = cnt;
    num[a].index = a;
    return ;
}

int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF){
        char **c = new char *[m];    //这里主要想用一下将二维数组当作参数传递,则需要动态申请,可以直接全局变量更好用
        for(int i = 0; i < m; i++){
            c[i] = new char[n+1];
        }
        for(int i = 0; i < m; i++){
            getchar();
            for(int j = 0; j < n; j++){
                scanf("%c", &c[i][j]);
            }
            cal(c, i, n);
        }
        sort(num, num+m, cmp);
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++)
                printf("%c", c[num[i].index][j]);
            printf("\n");
        }
        delete c;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值