POJ DNA Sorting 1007 ANSWER

Source: Internet
Author: User

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

Analysis:

The key to the

problem is how to solve the disorder of a sequence, the worst must be O (N2), here we can analyze the problem from the angle of induction, if we know the n-1 of the first letter sequence of disorder, then for the nth letter, we need to compare with the previous n-1 letters to update, the problem does not seem to change, Still the same complexity, but here we only ACGT four letters, we want to use a stronger induction to reduce the size of the problem, for the nth letter, we do not need to explicitly and the previous n-1 letter comparison, as long as we know that there are several a, a few C, a few g, a few t, we can directly update the disorder of the , if a, then the new disorder needs to add the number of CGT of the first n-1 letters, using a stronger induction to solve the problem!

#include  <iostream>using namespace std;int find_sortedness (char *seq,int  Length) { int numa=0,numc=0,numg=0,numt=0; int unsort=0; for (int i=0;i<length; i++)  {  if (seq[i]== ' A ')   {   unsort+= (NUMC+NUMG+NUMT);    numa++;  }  else if (seq[i]== ' C ')   {   unsort+= (numG+ NUMT);    numc++;  }  else if (seq[i]== ' G ')   {    unsort+=numT;   numG++;  }  else   {    Numt++;  } } return unsort;} Int main () { int length=0; int nums=0; cin>>length>>nums; char**  a=new char *[nums]; int* results=new int[nums]; int* print=new  Int[nums]; for (int i=0;i<nums;i++)  {  a[i]=new  Char[length];  for (int j=0;j<length;j++)   {   cin>>a[i][j];   }  results[i]=find_sortedness (a[i],length);   print[i]=i; } for (int  i=0;i<nums;i++)   for (int j=nums-1;j>i;j--)   {   if (results [J]<results[j-1])     {    int tmp=results[j];     results[j]=results[j-1];    results[j-1]=tmp;    tmp=print [J];    print[j]=print[j-1];    print[j-1]=tmp;   }   } for (int i=0;i<nums;i++)  {  for (int j=0;j<length;j++)    {   cout<<a[print[i]][j];  }  cout<<endl; }  return 0; }


This article is from "This and that" blog, please be sure to keep this source http://hereandthere.blog.51cto.com/7561107/1605819

POJ DNA Sorting 1007 ANSWER

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.