Poj 1007 DNA Sorting (Reverse Order Number), poj1007

Source: Internet
Author: User

Poj 1007 DNA Sorting (Reverse Order Number), poj1007
DNA Sorting

Time Limit:1000 MS   Memory Limit:10000 K
Total Submissions:83069   Accepted:33428

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 6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCAT

Sample Output

CCCGGGGGGAAACATGAAGGGATCAGATTTATCGATGCATTTTTGGCCAATTTGGCCAAA

Source

East Central North America 1998 A simple sorting question, the question is to find the number of reverse orders, first find the number of reverse orders for each segment of DNA sequence, then sort the number of reverse orders, and then output the NDA sequence after sorting; Here, there is a clever way to calculate the number of reverse orders, because there are only four letters in the question, so we use this particularity, we can get the method of O (n) to calculate the number of reverse orders, this requires us to accumulate more resources at ordinary times. It is convenient to merge and sort data or use a tree array to analyze actual problems; The following code calculates the number of reverse orders of O (n;
# Include <cstdio> # include <cstring> # include <algorithm> using namespace std; struct node {char s [100]; // store the DNA sequence int sum; // Number of reverse orders for storing each DNA sequence} a [100]; bool cmp (node x, node y) // comparison function {return x. sum <y. sum;} int count_inver (char * str, int len) // calculates the number of reverse orders {int I; int cnt = 0; int a [4] = {0 }; // use an array to save the number of times letters appear for (I = len-1; I> = 0; I --) {switch (str [I]) {case 'A': A [1] ++; a [2] ++; a [3] ++; break; case 'C ': a [2] ++; a [3] ++; cnt + = a [1]; break; case 'G': a [3] ++; cnt + = a [2]; break; case 'T': cnt + = a [3] ;}} return cnt ;}int main () {int m, n, i, j; scanf ("% d", & n, & m); for (I = 0; I <m; I ++) {scanf ("% s", a [I]. s); a [I]. sum = count_inver (a [I]. s, n) ;}sort (a, a + m, cmp); for (j = 0; j <m; j ++) printf ("% s \ n ", a [j]. s );}
Because the data volume of this question is not too large, you can simply use reverse order; The number of reverse orders is to check the number of numbers in front of this number, which is larger than the current number. A Dual Loop is directly used here;
# Include <iostream> # include <algorithm> using namespace std; struct f {int num; char w [50];} s [101]; bool cmp (struct f, struct f B) {return. num <B. num;} int main () {int I, len, n, j, k; cin> len> n; for (I = 0; I <n; I ++) {s [I]. num = 0; cin> s [I]. w; for (j = 1; j <len; j ++) for (k = 0; k <j; k ++) if (s [I]. w [j] <s [I]. w [k]) // returns the number of reverse orders s [I]. num ++;} sort (s, s + n, cmp); for (I = 0; I <n; I ++) cout <s [I]. w <endl; return 0 ;}




POJ-1007 DNA Sorting, AC

Algorithm has a built-in merge sort to ensure data stability. The stable_sort () parameter is the same as that of sort.

POJ 1007 DNA Sorting why does my code run right on my computer, but I wrote wrong answer when I submitted it?

# Include <stdio. h> # include <stdlib. h> # include <string. h>/* calculate the Confusion Function */int sortness (char * DNA, int length) {int m = 1, n = 1; int sort = 0; for (m = 0; m <length; m ++) for (n = m + 1; n <length; n ++) {if (DNA [m]> DNA [n]) sort ++;} return sort;}/* swap two string functions */void strChange (char * first, char * second) {char temp [100] = {0}; strcpy (temp, first); strcpy (first, second); strcpy (second, temp );} /* main function */int main () {int sortness (char *, int); int strLength = 0, strNumber = 0; int I = 0, k = 1, t = 0, sort = 0; int x = 0, y = 0, swaptemp; char DNA [102] [52] = {0}; int DNASort [102]; /* input a two-dimensional array */scanf ("% d", & strLength, & strNumber); for (I = 0; I <strNumber; I ++) {for (k = 0; k <strLength; k ++) scanf ("% c", & DNA [I] [k]); DNA [I] [strLength] = '\ 0';}/* calculates the clutter and copies it in DNASort */for (t = 0; t <strNumber; t ++) {sort = sortness (DNA [t], strLength); DNASort [t] = sort ;}for (x = 0; x <strNumber-1; x ++) {for (y = 0; y <strNumber-x-1; y ++) if (DNASort [y]> DNASort [y + 1]) {strChange (DNA [y], DNA [y + 1]); swaptemp = DNASort [y]; DNASort [y] = DNASort [y + 1]; DNASort [y + 1] = swaptemp ;}} for (I = 0; I <strNumber; I ++) {for (k = 0; k <strLength; k ++) printf ("% c", DNA [I] [k]); printf ("\ n ");}}

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.