DNA Sorting poj1007 解题报告

来源:互联网 发布:怎么看java源码 编辑:程序博客网 时间:2024/05/02 04:19

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

Sample Output

CCCGGGGGGAAACATGAAGGGATCAGATTTATCGATGCATTTTTGGCCAATTTGGCCAAA
解题思路:这题主要要看懂题意就KO了,知道了就很简单了,刚开始一直不知道是 什么回事的,一直不知怎么出那个结果的,最后Google一下就完全明白了,只是暴力求 出逆序和,然后排序,没 什么可以说的,也没什么复杂的算法,只是简单的一些操作,就随便在网上copy了 一个代码解决问题了,但还是要学会 结构体排序等等。    代码如下: 
Code:
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <string>  
  4.    
  5. using namespace std;  
  6.    
  7. struct dna  
  8. {  
  9.     int pos;  
  10.     int key;  
  11.     string str;  
  12. };  
  13.    
  14. /* sort比较函数 */  
  15. bool cmp(const dna &a, const dna &b)  
  16. {  
  17.     if (a.key != b.key)  
  18.     {  
  19.         return a.key < b.key;  
  20.     }  
  21.     else  
  22.     {  
  23.         return a.pos < b.pos;  
  24.     }  
  25. }  
  26.    
  27. int main()  
  28. {  
  29.     int n, m, count;  
  30.     dna inv[110];  
  31.     string str;  
  32.     cin >> n >> m;  
  33.    
  34.     /* 求逆序数对的个数 */  
  35.     for (int i = 0; i < m; i++)  
  36.     {  
  37.         cin >> str;  
  38.         count = 0;  
  39.         for (int j = 0; j < n - 1; j++)  
  40.         {  
  41.             for (int k = j + 1; k < n; k++)  
  42.             {  
  43.                 if (str[j] > str[k]) count++;  
  44.             }  
  45.         }  
  46.         /* 保存信息 */  
  47.         inv[i].key = count;  
  48.         inv[i].pos = i;  
  49.         inv[i].str = str;  
  50.     }  
  51.    
  52.     /* 按逆序数对大小/序号排序 */  
  53.     sort(inv, inv + m, cmp);  
  54.    
  55.     /* 输出结果 */  
  56.     for (int i = 0; i < m; i++)  
  57.     {  
  58.         cout << inv[i].str << endl;  
  59.     }  
  60.    
  61.     //system("pause");  
  62.     return 0;  
  63. }  


原创粉丝点击