POJ 1007 DNA Sorting(sort函数的使用)

Posted Reqaw’s Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1007 DNA Sorting(sort函数的使用)相关的知识,希望对你有一定的参考价值。

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 /*
 2 问题 输入每个字符串的长度n和字符串的个数,计算并将这些字符串的按照它的逆序数排序输出
 3 解题思路 将一个字符串和它的逆序数存入一个结构体数组中,使用sort按照逆序数排序输出即可。 
 4 */ 
 5 #include<string>
 6 #include<cstring>
 7 #include<iostream>
 8 #include<algorithm>
 9 using namespace std;
10 
11 struct Dstr{
12     string str;
13     int cou;
14 }Dstrs[110];
15 int unsortnum(char *str);
16 
17 bool cmp(struct Dstr a,struct Dstr b){
18     return a.cou<b.cou;
19 }
20 
21 int main()
22 {
23     int i,n,m;
24     char temp[60];
25     while(scanf("%d%d",&n,&m) != EOF){
26         for(i=0;i<m;i++){
27             scanf("%s",&temp);
28             Dstrs[i].str=temp;
29             Dstrs[i].cou=unsortnum(temp);
30         }
31         
32         sort(Dstrs,Dstrs+m,cmp);
33         
34         for(i=0;i<m;i++){
35             printf(Dstrs[i].str.c_str());
36             printf("\n");
37         }
38     }
39     return 0;
40 } 
41 
42 int unsortnum(char *str){
43     int len=strlen(str),i,j,un=0;
44     for(i=0;i<len-1;i++){
45         for(j=i+1;j<len;j++){
46             if(str[i] > str[j])    un++;
47         }
48     }
49     //printf("%s %d\n",str,un);
50     return un;
51 }

 

以上是关于POJ 1007 DNA Sorting(sort函数的使用)的主要内容,如果未能解决你的问题,请参考以下文章

poj 1007 DNA Sorting

poj1007 DNA Sorting

POJ 1007 DNA Sorting(sort函数的使用)

POJ 1007 DNA Sorting

A计划--POJ1007 DNA Sorting

POJ1007 DNA Sorting