Wednesday, April 20, 2011

612 - DNA Sorting

#include<iostream>
#include<cstdio>
#include<algorithm>
#define M 55
using namespace std;

struct DNA
{
    int s;
    char dna[M];
};
int CountSort(char[],int n);
void sort(struct DNA[],int[],int);

int main()
{
    int t,m,n,i,R[105];
    struct DNA D[105];
    //freopen("in.txt","r",stdin);

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d\n",&n,&m);

        for(i=0;i<m;i++)
        {
            gets(D[i].dna);
            D[i].s=CountSort(D[i].dna,n);
        }
        sort(D,R,m);
        for(i=0;i<m;i++)
            printf("%s\n",D[R[i]].dna);
        if(t)printf("\n");
    }
    return 0;
}

int CountSort(char dna[],int n)
{
    int i,j;
    int cnt=0;
    for (i=0;i<n-1;i++)
        for (j=i+1;j<n;j++)
            if (dna[i]>dna[j])
                cnt++;
    return cnt;
}

void sort(struct DNA D[],int r[],int m)
{
    int i,j,row,min;
    for (i=0;i<m;i++)
    {
        min=1000000;
        for (j=0;j<m;j++)
            if (D[j].s<min)
                min=D[j].s,row=j;
        r[i]=row;D[row].s=1000000;
    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.