POJ1007

来源:互联网 发布:java难点 编辑:程序博客网 时间:2024/06/06 00:34

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 sequenceDAABEC”, 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 sequenceZWQM” 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'', frommost 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

#include<stdio.h>#include<stdlib.h>#include<string.h>struct Str{    char a[55];    int n;}str[101],t;int getinversion(char *a,int a_length){int i,j;int count=0;for( i=0;i<a_length;i++){    for( j=i+1;j<a_length;j++)    {        if(a[i]>a[j])            count++;    }}return count;}int main(){int i,j;int length,num;scanf("%d %d",&length,&num);getchar();for( i=0;i<num;i++)    {        gets(str[i].a);        str[i].n=getinversion(str[i].a,length);    }for( i=0;i<num-1;i++)    for( j=0;j<num-1-i;j++)    {        if(str[j].n >str[j+1].n )            {                t=str[j];                str[j]=str[j+1];                str[j+1]=t;            }    }for( i=0;i<num;i++)    puts(str[i].a);} 
原创粉丝点击