POJ 1007 DNA Sorting

DNA Sorting
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 103246   Accepted: 41368

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 ``least sorted''. Since two strings can be equally sorted, then output them according to the orginal order.

Sample Input

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

 1     Source Code
 2     Problem: 1007        User: zhaopeng938
 3     Memory: 304K        Time: 16MS
 4     Language: C++        Result: Accepted
 5 
 6 Source Code
 7 
 8 #include<iostream>
 9 //#include<fstream>
10 #include<string>
11 #include<vector>
12 using namespace std;
13 int main()
14 {
15     //ifstream in("test.txt");
16     vector<string> arr;
17     vector<int> sort;
18     int n,m;
19     cin>>n>>m;
20     int i,j,k;
21     for(i=0;i<m;i++)
22     {
23         string temp;
24         cin>>temp;
25         arr.push_back(temp);
26         int num=0;
27         for(j=0;j<arr[i].size()-1;j++)
28             for(k=j+1;k<arr[i].size();k++)
29                 if(arr[i][j]>arr[i][k])
30                     num++;
31         sort.push_back(num);
32     }
33     //for(i=0;i<arr.size();i++)
34         //cout<<arr[i]<<" "<<sort[i]<<endl;
35     for(i=sort.size()-1;i>0;i--)
36     {
37         for(j=0;j<i;j++)
38         {
39             if(sort[j]>sort[j+1])
40             {
41                 int ls_num;
42                 ls_num=sort[j];
43                 sort[j]=sort[j+1];
44                 sort[j+1]=ls_num;
45 
46                 string ls_temp;
47                 ls_temp=arr[j];
48                 arr[j]=arr[j+1];
49                 arr[j+1]=ls_temp;
50             }
51         }
52     }
53     for(i=0;i<arr.size();i++)
54         cout<<arr[i]<<endl;
55     return 0;
56 }

 

posted on 2017-09-19 14:43  遇一人倾国倾城  阅读(123)  评论(0编辑  收藏  举报

导航