POJ 1007 DNA Sorting
DNA Sorting
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 63738 | Accepted: 25165 |
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.
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
Source
思路是先用归并求逆序数,然后用一种稳定排序(我用的bubblesort,在代码的63-73行)给这些字符串按逆序数排序。写的代码比较乱而且依赖STL比较严重…
以上是归并求逆序数的算法伪码。
程序用了INT_MAX结果忘了包含<climits>,还Compile Error一次…囧。
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 #include <cstdlib>
5 #include <cstdio>
6 #include <climits>
7
8 using namespace std;
9
10 int inversion(0);
11
12 void merge(string &arr,unsigned begin,unsigned mid,unsigned end)
13 {
14 unsigned n1 = mid-begin+1;
15 unsigned n2 = end-mid;
16 vector<int> left(n1+1),right(n2+1);
17 for(unsigned i(0);i != n1;++i)
18 left[i] = arr[begin+i]-'A';
19 for(unsigned j(0);j != n2;++j)
20 right[j] = arr[mid+1+j]-'A';
21 left[n1] = right[n2] = -INT_MAX;
22 for(unsigned i(0),j(0),cur(begin);cur <= end;++cur)
23 {
24 if(left[i] > right[j])
25 {
26 arr[cur] = left[i]+'A';
27 i++;
28 inversion += (n2-j);
29 }
30 else
31 {
32 arr[cur] = right[j]+'A';
33 j++;
34 }
35 }
36 }
37
38 void mergeSort(string &arr,unsigned begin,unsigned end)
39 {
40 if(begin < end)
41 {
42 unsigned mid = (begin+end)/2;
43 mergeSort(arr,begin,mid);
44 mergeSort(arr,mid+1,end);
45 merge(arr,begin,mid,end);
46 }
47 }
48
49 int main(void)
50 {
51 vector< pair<string,int> > vec;
52 int len,count;
53 string str,backup;
54 cin >> len >> count;
55 for(int i(0);i != count;++i)
56 {
57 cin >> str;
58 backup = str;
59 mergeSort(str,0,str.size()-1);
60 vec.push_back(make_pair(backup,inversion));
61 inversion = 0;
62 }
63 for(unsigned i(0);i != vec.size();++i)
64 for(unsigned j(i+1);j != vec.size();++j)
65 {
66 if(vec[i].second > vec[j].second)
67 {
68 pair<string,int> bk;
69 bk.first = vec[i].first;bk.second = vec[i].second;
70 vec[i].first = vec[j].first;vec[i].second = vec[j].second;
71 vec[j].first = bk.first;vec[j].second = bk.second;
72 }
73 }
74 for(unsigned i(0);i != vec.size();++i)
75 cout << vec[i].first << endl;
76
77 return 0;
78 }