码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-03 19:15:44      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:计算   des   put   temp   positive   ica   lines   with   scan   

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函数的使用)

标签:计算   des   put   temp   positive   ica   lines   with   scan   

原文地址:https://www.cnblogs.com/wenzhixin/p/8710165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!