POJ 1007 DNA Sorting

题目:http://poj.org/problem?id=1007

   根据逆序数排序

#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>

using namespace std;

struct DNA
{
    string str;
    int rank;
}dna[105];

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

int main()
{
    int n,m;
    cin>>n>>m;
    for (int i = 0; i < m; i++)
    {
        cin>>dna[i].str;
        dna[i].rank=0;
        for (int j = 0; j < n; j++)
        {
            for (int k = j+1; k < n; k++)
            {
                if(dna[i].str[j]>dna[i].str[k])  dna[i].rank++;
            }
        }
    }

    qsort(dna,m,sizeof(DNA),cmp);
    
    for(int i=0;i<m;i++)
    {
        cout<<dna[i].str<<endl;
    }

    return 0;
}

 

posted @ 2013-01-31 18:34  Daniel Qiu  阅读(140)  评论(0编辑  收藏  举报