杭电1379--DNA Sorting

DNA Sorting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2185    Accepted Submission(s): 1064


Problem 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.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is 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.

 

 

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 (1 < 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 ``least sorted''. If two or more strings are equally sorted, list them in the same order they are in the input file.
 

 

Sample Input
1
10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
 

 

Sample Output
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
 

 

Source
 

 

Recommend
Eddy   |   We have carefully selected several similar problems for you:   1328  1321  1062  1020  1073 
 
//输入块之间有空行, 输出块之间也有空行;
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 struct ac
 7 {
 8     int s;
 9     char str[55];
10 };
11 ac num[110];
12 
13 bool cmp(ac a, ac str)
14 {
15     return a.s < str.s;
16 }
17 
18 int main()
19 {
20     char ch[55];
21     int sum, total = 0;
22     int t, i, j, k, m, n;
23     scanf("%d", &t);
24     while(t--)
25     {
26         if(total != 0)
27         printf("\n"); 
28         scanf("%d %d", &n, &m);
29         total++ ;
30         for(i=0; i<m; i++)
31         {
32             sum = 0;
33             scanf("%s", ch);
34             //puts(ch);
35             strcpy(num[i].str, ch);
36             //puts(num[i].str);
37             for(k=0; k<n-1; k++)
38             {
39                 for(j=k+1; j<n; j++)
40                 {
41                     if(ch[k] > ch[j])
42                     sum++;
43                     //printf("%d %d\n", k, sum);
44                 }
45             }
46             num[i].s = sum;
47         }
48         sort(num, num+m, cmp);
49         for(i=0; i<m; i++)
50         printf("%s\n", num[i].str);
51         if(t!=0)
52         printf("\n");
53     }
54     return 0;
55 }

 

你可能感兴趣的:(sort)