POJ 1007 DNA Sorting

DNA Sorting
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 74133   Accepted: 29661

Description

One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)---it is nearly sorted---while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be---exactly the reverse of sorted). 

You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length. 

Input

The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.

Output

Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. Since two strings can be equally sorted, then output them according to the orginal order.

Sample Input

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
题目大意:给出一组DNA序列,按序列中字母的逆序数从小到大排列,如果逆序数相同,则按输入顺序打印结果。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;

char temp[55];
int nCount = 0;

typedef struct
{
    int c;
    char str[55];
}S;

bool cmp(const S& s1, const S& s2)
{
    return s1.c < s2.c;
}

void Merge(char s[], int low, int mid, int high)
{
    int index1 = low;
    int index2 = mid + 1;
    int index3 = 0;
    while(index1 <= mid && index2 <= high)
    {
        if (s[index1] <= s[index2])
        {
            temp[index3++] = s[index1++];
        }
        else
        {
            temp[index3++] = s[index2++];
            nCount += mid + 1 - index1;
        }
    }
    while(index1 <= mid)
    {
        temp[index3++] = s[index1++];
    }
    while(index2 <= high)
    {
        temp[index3++] = s[index2++];
    }
    for (int i = 0, j = low; j <= high; i++, j++)
    {
        s[j] = temp[i];
    }
}

void MergeSort(char s[], int low, int high)
{
    if (low < high)
    {
        int mid = (low + high) / 2;
        MergeSort(s, low, mid);
        MergeSort(s, mid + 1, high);
        Merge(s, low, mid, high);
    }
}

int main()
{
    char str[55];
    int m, n;
    S s[105];
    scanf("%d%d", &m, &n);
    for (int i = 0; i < n; i++)
    {
        nCount = 0;
        scanf("%s", str);
        strcpy(s[i].str, str);
        MergeSort(str, 0, m - 1);
        s[i].c = nCount;
    }
    sort(s, s + n, cmp);
    for (int i = 0; i < n; i++)
    {
        printf("%s
", s[i].str);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lzmfywz/p/3192262.html