Hangzhou Electric 1379 (DNA sorting) Java Object-oriented programming

Source: Internet
Author: User

Click to open the chain Hangzhou electric 1379

problem DescriptionOne 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 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 the 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 Input
6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCAT

Sample Output
Cccggggggaaacatgaagggatcagatttatcgatgcatttttggccaatttggccaaa

Object-oriented programming, although seemingly troublesome, but clear-minded.

Import java.util.*;class main{public static void Main (string[] args) {int n,m,i,j,t; String str; Scanner sc=new Scanner (system.in); T=sc.nextint (); while (t-->0) {n=sc.nextint (); M=sc.nextint (); Sc.nextline (); Stringsort[] strs=new stringsort[m];for (i=0;i<m;i++) {str=sc.nextline (); strs[i]=new StringSort (str);} for (i=0;i<m;i++) {strs[i].measure ();} Sort (STRs); for (i=0;i<m;i++) {System.out.println (STRS[I].STR);}}} public static void Sort (stringsort[] STRs) {for (int i=0;i<strs.length-1;i++) {int minindex=i;for (int j=i+1;j< strs.length;j++) {if (strs[j].count<strs[minindex].count) {minindex=j;}} if (minindex!=i) {Stringsort temp=strs[i];strs[i]=strs[minindex];strs[minindex]=temp;}}}} Class stringsort{string Str;int count;public stringsort (String str) {this.str=str;this.count=0;} public void measure () {in (int i=0;i<str.length () -1;i++) {for (int j=i+1;j<str.length (); j + +) {if (Str.charat (i) >str.charat (j)) {count++;}}}}




Hangzhou Electric 1379 (DNA sorting) Java Object-oriented programming

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.