I. Question Information
DNA Sorting
Time Limit: 1000 MS Memory Limit: 10000 K
Total Submissions: 68122 Accepted: 27076
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
ATCGATGCAT
Sample Output
CCCGGGGGGA
Aacatga.pdf
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
Ii. Algorithm Analysis
1. Enumeration
This question is actually to calculate the number of reverse orders of a sequence. For more information about the concept of reverse order number, see. The most direct method is to find a number of elements after the element in the sequence, which is smaller than the element. We call this number as the reverse order of an element. The sum of all elements in a sequence in the descending order is the number of reverse orders in a sequence.
In the enumeration method, the time complexity of the reverse sequence is O (n ^ 2). In addition, the time complexity is also O (n ^ 2 * mlogm ).
Note that the question must be Since two strings can be equally sorted, then output them according to the orginal order. that is, the output sequence is the same as the input sequence for two sequences with the same number of reverse orders. This requires sorting the sequence, and should adopt stable sorting. Use the algorithm stable_sort in C ++ STL.
2. Merge and sort
Simply finding the number of reverse orders, merging and sorting is a good method. In the process of merging and sorting, adding the record element exchange times code can find the number of reverse orders. Time complexity can be reduced to O (nlogn ). The total time complexity can be reduced to O (nlogn * mlogm ).
Iii. Reference Code
1. Enumeration Method C ++ code
[Cpp]
# Include <iostream>
# Include <vector>
# Include <string>
# Include <algorithm>
# Include <windows. h>
Using namespace std;
Size_t countUnsortedness (const string & s)
{
Int sum = 0;
For (int I = 0; I <s. size (); I ++)
For (int j = I + 1; j <s. size (); j ++)
If (s [j] <s [I])
Sum ++;
Return sum;
}
Bool comp (const string & s1, const string & s2)
{
Return countUnsortedness (s1) <countUnsortedness (s2 )? True: false;
}
Int main ()
{
Vector <string> DNAs;
Int n, m;
Cin> n> m;
For (string s; m --; cin> s, DNAs. push_back (s ));
Stable_sort (DNAs. begin (), DNAs. end (), comp );
For (int I = 0; I <DNAs. size (); I ++)
Cout <DNAs [I] <endl;
System ("pause ");
Return 0;
}
2. Merge and sort C ++ code