zoukankan      html  css  js  c++  java
  • poj 1007:DNA Sorting(水题,字符串逆序数排序)

    DNA Sorting
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 80832   Accepted: 32533

    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

    Source

     
      水题,字符串排序
      题意:给你m个长度为n的字符串,要求让你按照字符串的逆序数进行稳定排序。
        所谓逆序数,就是给定一个排列和一个标准次序,如果这个排列中有两个元素与标准次序不同,则称这是一个逆序,这个排列的所有逆序总数称为这个排列的逆序数。
      思路:定义一个结构体存储字符串和字符串的逆序数,在主程序里,输入字符串之后计算每一个字符串的逆序数。然后用sort进行排序,最后输出排序后的字符串。
      代码
     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 struct Str{
     5     char s[51];
     6     int n;  //逆序数个数
     7 };
     8 bool cmp(const Str &a,const Str &b)
     9 {
    10     if(a.n<b.n)
    11         return 1;
    12     return 0;
    13 }
    14 int main()
    15 {
    16     int n,m,i,j;
    17     Str str[101];
    18     while(cin>>n>>m){
    19         for(i=1;i<=m;i++)   //输入
    20             cin>>str[i].s;
    21         for(i=1;i<=m;i++){
    22             int num=0;
    23             int A=0,C=0,G=0;
    24             for(j=n-1;j>=0;j--){
    25                 switch(str[i].s[j]){    //计算逆序数
    26                     case 'A':A++;break;
    27                     case 'C':C++;num+=A;break;
    28                     case 'G':G++;num+=A;num+=C;break;
    29                     case 'T':num+=A;num+=C;num+=G;break;
    30                     default:break;
    31                 }
    32             }
    33             str[i].n = num;
    34         }
    35         sort(str+1,str+m+1,cmp);
    36         for(i=1;i<=m;i++)
    37             cout<<str[i].s<<endl;
    38     }
    39     return 0;
    40 }

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    CODE
    JS中如何进行对象的深拷贝
    js数组操作---改变原数组和不改变原数组的方法整理
    echarts做双柱图
    如何在react中使用OrgChart?
    如何让背景透明?
    如何使用css选择器隐藏滚动条?
    input 修改placeholder中颜色和字体大小
    在input、textarea 标签的placeholder中实现换行的方法
    React Hooks异步操作防踩坑指南
  • 原文地址:https://www.cnblogs.com/yym2013/p/3770387.html
Copyright © 2011-2022 走看看