Description
One measure of ' unsortedness ' in a sequence is the number of the pairs of entries, which is out of order with respect to each Other. For instance, with the letter sequence "Daabec", this measure is 5, since D was greater than four letters E is greater than one letter to it right. This measure was called the number of inversions in the sequence. The sequence ' AACEDGG ' have only one inversion (E and D)---it was nearly sorted---while the sequence "ZWQM" has 6 invers Ions (it is as unsorted as can---exactly the reverse of sorted).
You is 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 is of the same length.
Input
The first line contains the integers:a positive integer n (0 < n <=) giving the length of the strings; and a positive integer m (0 < M <=) giving the number of strings. These is 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 strings can be equally sorted and then output them according to the orginal order.
Sample Input
Ten 6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCAT
Sample Output
Cccggggggaaacatgaagggatcagatttatcgatgcatttttggccaatttggccaaa
This problem uses array techniques to calculate the inverse number as shown in function f
Using structs to associate reverse order numbers with strings
Use the Stable_sort function in algorithm to sort the same, not change the original sequence!!
#include <iostream>#include<algorithm>using namespacestd;structdna{Charstr[ -]; intnum;} d[ the];BOOLCMP (DNA A,dna b) {returna.num<B.num; } intFCharS[],intN) { inta[5]={0,0,0,0},m=0; for(inti=n-1; i>=0; i--){ Switch(S[i]) { Case 'A': a[1]++; a[2]++; a[3]++; Break; Case 'C': a[2]++; a[3]++; M+=a[1]; Break; Case 'G': a[3]++; M+=a[2]; Break; Case 'T': M+=a[3]; Break; } } returnm;}intMain () {intn,m; CIN>>n>>m; for(intI=0; i<m;i++){ for(intj=0; j<n;j++) cin>>D[i].str[j]; D[i].num=f (d[i].str,n); } stable_sort (D,d+m,cmp); for(intI=0; i<m;i++) { for(intj=0; j<n;j++) cout<<D[i].str[j]; cout<<Endl; } //System ("pause"); return 0;}
E-merge sort to find the reverse order number