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 ''ast sorted''. Since two strings can be equally sorted, then output them according to the orginal order.
Sample Input
10 6
Aacatga.pdf
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCATSample Output
CCCGGGGGGA
Aacatga.pdf
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAASource
East Central North America 1998
[Cpp]
# Include <iostream>
# Define INF 0 xffffff // defines the maximum address
Using namespace std;
Char str [200] [200]; // two-dimensional array writes the value of each character in the string
Int num [200]; // The value of the number of reverse orders
Int main ()
{
Int m, n;
Memset (num, 0, sizeof (num ));
Cin> n;
Cin> m;
Num [0] = INF; // initialize num [0] as the maximum address, so that you can select the sorting method to output the string array.
For (int I = 1; I <= m; I ++)
{
Cin> str [I];
// Obtain the number of reverse orders
For (int j = 0; j <n; j ++)
{
For (int k = j + 1; k <n; k ++)
{
If (str [I] [j]> str [I] [k])
{
Num [I] ++;
}
}
}
}
Int p = 0;
// Select the sorting method to output the String Array
For (int I = 1; I <= m; I ++)
{
For (int j = 1; j <= m; j ++)
{
If (num [j] <num [p])
{
P = j;
}
}
Cout <str [p] <endl;
Num [p] = INF; // set the current num [p] to the maximum address to facilitate the comparison of the next loop
}
System ("pause ");
Return 0;
}
# Include <iostream>
# Define INF 0 xffffff // defines the maximum address
Using namespace std;
Char str [200] [200]; // two-dimensional array writes the value of each character in the string
Int num [200]; // The value of the number of reverse orders
Int main ()
{
Int m, n;
Memset (num, 0, sizeof (num ));
Cin> n;
Cin> m;
Num [0] = INF; // initialize num [0] as the maximum address, so that you can select the sorting method to output the string array.
For (int I = 1; I <= m; I ++)
{
Cin> str [I];
// Obtain the number of reverse orders
For (int j = 0; j <n; j ++)
{
For (int k = j + 1; k <n; k ++)
{
If (str [I] [j]> str [I] [k])
{
Num [I] ++;
}
}
}
}
Int p = 0;
// Select the sorting method to output the String Array
For (int I = 1; I <= m; I ++)
{
For (int j = 1; j <= m; j ++)
{
If (num [j] <num [p])
{
P = j;
}
}
Cout <str [p] <endl;
Num [p] = INF; // set the current num [p] to the maximum address to facilitate the comparison of the next loop
}
System ("pause ");
Return 0;
}