uva 612 DNA Sorting

原题:
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 of the input is an integer M, then a blank line followed by M datasets. There is a blank
line between datasets.The first line of each dataset 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
For each dataset, output the list of input strings, arranged from “most sorted” to “least sorted”. If two or more strings are equally sorted, list them in the same order they are in the input file.Print a blank line between consecutive test cases.

Sample Input
1
10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

Sample Output
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

大意:(来自lucky 猫)
在一個字串中,「未排序」的程度是以各字元間彼此的大小關係來計算的。例如在字串 DAABEC中,「未排序」的程度為 5,因為D比它右邊的4個字元大,E比它右邊的1個字元大。而字串AACEDGG「未排序」的程度為 1(幾乎是快排序好的,唯一的未排序發生在E和D之間),字串ZYXW「未排序」的程度為 6(剛好是完全排序的相反)。

現在你的任務是為許多的DNA字串來做排序。每個字串中僅含有A,C,G和T這4種字元。排序的原則是根據各字串「未排序」的程度,由小到大輸出。在這裡每個字串的長度均相同。

Input

輸入的第一列有一個整數代表以下有幾組測試資料。每組測試資料的第一列含有2個正整數 n(0 < n <= 50)和 m(0 < m <= 100),n 代表字串的長度,m 代表字串的數目。接下來的 m 列,每列有一個長度為 n 的字串。

第一列及第一組測試資料,以及各組測試資料間均有一空白列。請參考Sample Input。

Output

對每組測試資料按照「未排序」的程度,由小到大輸出各字串。假如有不只2個字串「未排序」的程度相同,則按照它們在輸入中的順序輸出。

各組測試資料之間請輸出一空白列,輸出格式請參考Sample Output。

#include<bits/stdc++.h>
using namespace std;

int n,m;
//fstream in,out;
struct node
{
    string ss;
    int mark;
};
node s[101];
int get_index(const string &ss)
{
    int inde=0;
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(ss[i]>ss[j])
                inde++;
        }
    }
    return inde;
}
bool cmp(const node &s1,const node &s2)
{
    int index1=0,index2=0;
    index1=get_index(s1.ss);
    index2=get_index(s2.ss);
    if(index1!=index2)
        return index1<index2;
    return s1.mark<s2.mark;
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
// in.open("data.txt");
// out.open("input.txt");
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=1;i<=m;i++)
        {
            cin>>s[i].ss;
            s[i].mark=i;
        }
        sort(s+1,s+1+m,cmp);
        for(int i=1;i<=m;i++)
            cout<<s[i].ss<<endl;
        if(t!=0)
            cout<<endl;

    }
// in.close();
// out.close();
    return 0;
}

解答:
超级简单题,不过注意输出! 就是让你找逆序的字母,然后按照你每个字符串逆序字母的数量排序,相同逆序数的按照编号排序…

你可能感兴趣的:(uva)