DNA sorting
Problem descriptionone measure of ' unsortedness ' in a sequence was the number of pairs of entries that was out of order W ith respect to all 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 are nearly sorted--while the sequence ' ZWQM ' has 6 Inversio NS (it is as unsorted as can be--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.
This problem contains multiple test cases!
The first line of a multiple input was an integer N and then a blank line followed by N input blocks. Each input block was in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Inputthe first line contains, integers:a positive integer n (0 < n <=) giving the length of the strings; and a positive integer m (1 < m <=) giving the number of strings. These is followed by M-lines, each containing a string of length n.
Outputoutput the list of input strings, arranged from "most sorted" to "least sorted". If or more strings is equally sorted, list them in the same order they is in the input file.
Sample Input110 6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCAT
Sample OUTPUTCCCGGGGGGAAACATGAAGGGATCAGATTTATCGATGCATTTTTGGCCAATTTGGCCAAA
the problem of silly violence in order to reverse the number can be, I use the merger sort to beg. Code:
1#include <cstdio>2#include <cstring>3#include <malloc.h>4#include <algorithm>5 using namespacestd;6 7 intans;8 9 structStrTen { One Chars[ the]; A Charsort_s[ the]; - intNum; - }; the - BOOLcmpstructStr A,structstr b) - { - returna.num<B.num; + } - + voidMergearray (CharA[],intLintMid,intRChartemp[]) A { at intk=0; - intI=l,j=mid+1; - intm=mid,n=R; - while(i<=m&&j<=N) - { - if(a[i]<=A[j]) intemp[k++]=a[i++]; - Else to { +temp[k++]=a[j++]; -ans+=m-i+1; the } * } $ while(i<=m)Panax Notoginsengtemp[k++]=a[i++]; - while(j<=N) thetemp[k++]=a[j++]; + for(i=0; i<k;i++) Aa[l+i]=Temp[i]; the } + - voidMergeSort (CharA[],intLintRChartemp[]) $ { $ if(l<R) - { - intMid= (L+R)/2; the mergesort (a,l,mid,temp); -MergeSort (a,mid+1, r,temp);Wuyi Mergearray (a,l,mid,r,temp); the } - } Wu - intMain () About { $ structSTR s[ -]; - inti,m,n,t; -scanf"%d",&t); - while(t--) A { +scanf"%d%d",&m,&n); the for(i=0; i<n;i++) -scanf"%s", S[I].S); $ for(i=0; i<n;i++) the { theans=0; the intlen=strlen (S[I].S); the strcpy (S[I].SORT_S,S[I].S); - Char*p= (Char*)malloc(sizeof(Char) * (len+1)); inMergeSort (s[i].sort_s,0, len-1, p); theS[i]. num=ans; the Free(p); About } theSort (s,s+n,cmp); the for(i=0; i<n;i++) theprintf"%s\n", S[I].S); + } - return 0; the}
HDU 1379 DNA Sorting (reverse order number)