DNA sorting
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:67603 |
|
Accepted:26858 |
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 ''ast sorted''. All
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 aacatgaaggttttggccaatttggccaaagatcagatttcccggggggaatcgatgcat
Sample output
Cccggggggaaacatgaagggatcagatttatcgatgcatttttggccaatttggccaaa
Source
# Include <iostream> # Include <Algorithm> Using Namespace STD; typedef Struct { String DNA; Int Count;} DNA; DNA [ 101 ]; Int CMP ( Const Void *, Const Void * B) {DNA * AA = (DNA * ) A; DNA * BB = (DNA * ) B; Return Aa-> count-BB->Count ;} Int Main (){ Int N, m; Char C; CIN >>N> M; For ( Int I = 0 ; I <m; I ++ ) {CIN > DNA [I]. DNA; DNA [I]. Count = 0 ; For ( Int J = 0 ; J <n; j ++ ) For ( Int K = J + 1 ; K <n; k ++ ){ If (DNA [I]. DNA [J]> DNA [I]. DNA [k]) DNA [I]. Count ++ ;}} Qsort (DNA, m, Sizeof (DNA [0 ]), CMP ); For ( Int I = 0 ; I <m; I ++ ) Cout <DNA [I]. DNA < Endl; Return 0 ;}